<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">






  
  
  <link rel="stylesheet" media="all" href="/lib/Han/dist/han.min.css?v=3.3">




<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.png?v=5.1.4">


  <link rel="mask-icon" href="/images/favicon.png?v=5.1.4" color="#222">





  <meta name="keywords" content="windows - C++," />





  <link rel="alternate" href="/atom.xml" title="平步青云win" type="application/atom+xml" />






<meta name="description" content="windows编程学习笔记。 参考资料： [1] https://fishc.com.cn/forum.php?mod=forumdisplay&amp;amp;fid=255&amp;amp;filter=typeid&amp;amp;typeid=420 [2] https://www.bilibili.com/video/BV1us411A7UE?p=1">
<meta name="keywords" content="windows - C++">
<meta property="og:type" content="article">
<meta property="og:title" content="windows编程">
<meta property="og:url" content="https://zxpgo.github.io/2020/05/01/windows编程/index.html">
<meta property="og:site_name" content="平步青云win">
<meta property="og:description" content="windows编程学习笔记。 参考资料： [1] https://fishc.com.cn/forum.php?mod=forumdisplay&amp;amp;fid=255&amp;amp;filter=typeid&amp;amp;typeid=420 [2] https://www.bilibili.com/video/BV1us411A7UE?p=1">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/20200430162129.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/20200430175111.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/20200430175504.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/11111111.jpg">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/20200501001240.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200504163819609.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200505163631841.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200505163410839.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200505164145565.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506114157779.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506114557378.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506143425257.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506143458506.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506144228796.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506144831002.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506223947680.png">
<meta property="og:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200507154425806.png">
<meta property="og:updated_time" content="2020-05-07T07:45:31.589Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="windows编程">
<meta name="twitter:description" content="windows编程学习笔记。 参考资料： [1] https://fishc.com.cn/forum.php?mod=forumdisplay&amp;amp;fid=255&amp;amp;filter=typeid&amp;amp;typeid=420 [2] https://www.bilibili.com/video/BV1us411A7UE?p=1">
<meta name="twitter:image" content="https://raw.githubusercontent.com/zxpgo/images/master/img/20200430162129.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"right","display":"post","offset":10,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'PAO8LM7QB1',
      apiKey: '',
      indexName: 'Blog',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://zxpgo.github.io/2020/05/01/windows编程/"/>





  <title>windows编程 | 平步青云win</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?7a4517a3ce6d7c50203655d056f01ac3";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-right page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">平步青云win</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-随笔">
          <a href="/sui" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            随笔
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
			
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://zxpgo.github.io/2020/05/01/windows编程/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="zxp">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="平步青云win">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">windows编程</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-05-01T17:24:24+08:00">
                2020-05-01
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/windows编程/" itemprop="url" rel="index">
                    <span itemprop="name">windows编程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2020/05/01/windows编程/" class="leancloud_visitors" data-flag-title="windows编程">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        <p>windows编程学习笔记。</p>
<p>参考资料：</p>
<p>[1] <a href="https://fishc.com.cn/forum.php?mod=forumdisplay&amp;fid=255&amp;filter=typeid&amp;typeid=420" target="_blank" rel="noopener">https://fishc.com.cn/forum.php?mod=forumdisplay&amp;fid=255&amp;filter=typeid&amp;typeid=420</a></p>
<p>[2] <a href="https://www.bilibili.com/video/BV1us411A7UE?p=1" target="_blank" rel="noopener">https://www.bilibili.com/video/BV1us411A7UE?p=1</a><a id="more"></a></p>
<p>第一个程序</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> WINAPI <span class="title">WinMain</span><span class="params">(HINSTANCE hInstance, </span></span></span><br><span class="line"><span class="function"><span class="params">                   HINSTANCE hPrevInstance, </span></span></span><br><span class="line"><span class="function"><span class="params">                   PSTR szCmdLIne, </span></span></span><br><span class="line"><span class="function"><span class="params">                   <span class="keyword">int</span> iCmdShow)</span> </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	MessageBox(<span class="literal">NULL</span>, TEXT(<span class="string">"Hello world"</span>), TEXT(<span class="string">"First"</span>), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2);</span><br><span class="line">    <span class="comment">//         父窗口句柄   内容                 标题         标志          </span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="Unicode解决方案"><a href="#Unicode解决方案" class="headerlink" title="Unicode解决方案"></a>Unicode解决方案</h1><p>ASCII：American标准，只有7位。</p>
<p>双字节字符集（DBCS）：ANSI标准，用两个字节来存放。</p>
<p>Unicode解决方案：</p>
<ul>
<li>统一所有的字符使用两个字节来存放</li>
<li>将所有国家的语言都放到一个字符集里</li>
</ul>
<p><code>wchar_t</code>就是一个</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;wchar.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;locale.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="keyword">wchar_t</span> c = L'爱<span class="string">';</span></span><br><span class="line">	wchar_t str[] = L"我爱中国";</span><br><span class="line">	setlocale(LC_ALL, <span class="string">"Chs"</span>);</span><br><span class="line">	wprintf(<span class="string">L"%lc, %ls\n"</span>, c, str);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> WINAPI <span class="title">WinMain</span><span class="params">(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLIne, <span class="keyword">int</span> iCmdShow)</span> </span>&#123;</span><br><span class="line">	TCHAR *szContent = TEXT(<span class="string">"这是我的一个程序"</span>);</span><br><span class="line">	<span class="keyword">static</span> TCHAR szCaption[] = TEXT(<span class="string">"打招呼"</span>);</span><br><span class="line">	MessageBox(<span class="literal">NULL</span>, szContent, szCaption, MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在Unicode编码下，TEXT就相等于L，即：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">TEXT(<span class="string">"打招呼"</span>);</span><br><span class="line"><span class="comment">//等同于</span></span><br><span class="line"><span class="string">L"打招呼"</span></span><br></pre></td></tr></table></figure>
<p>在Unicode编码下，TCHAR就相等于<code>wchar_t</code>。</p>
<h1 id="窗口"><a href="#窗口" class="headerlink" title="窗口"></a>窗口</h1><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND, UINT, WPARAM, LPARAM)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> WINAPI <span class="title">WinMain</span><span class="params">(HINSTANCE hInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	HINSTANCE hPrevInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	PSTR szCmdLine,</span></span></span><br><span class="line"><span class="function"><span class="params">	<span class="keyword">int</span> iCmdShow)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> TCHAR * szAppName = TEXT(<span class="string">"MyWindows"</span>);</span><br><span class="line">	HWND hwnd;            <span class="comment">//句柄，特殊的指针，找到窗口的标识</span></span><br><span class="line">	MSG msg;              <span class="comment">//消息结构，windwos核心就是消息机制</span></span><br><span class="line">	WNDCLASS wndclass;    <span class="comment">//窗口类结构</span></span><br><span class="line"></span><br><span class="line">	wndclass.style = CS_HREDRAW | CS_VREDRAW;                     <span class="comment">//窗口风格, CS_HERDRAW表示水平调整窗口会发生重绘</span></span><br><span class="line">	wndclass.lpfnWndProc = WndProc;                               <span class="comment">//窗口过程</span></span><br><span class="line">	wndclass.cbClsExtra = <span class="number">0</span>;                                      <span class="comment">//预留的额外空间</span></span><br><span class="line">	wndclass.cbWndExtra = <span class="number">0</span>;                                      <span class="comment">//</span></span><br><span class="line">	wndclass.hInstance = hInstance;                               <span class="comment">//实例句柄</span></span><br><span class="line">	wndclass.hIcon = LoadIcon(<span class="literal">NULL</span>, IDI_APPLICATION);             <span class="comment">//图标</span></span><br><span class="line">	wndclass.hCursor = LoadCursor(<span class="literal">NULL</span>, IDC_ARROW);               <span class="comment">//光标</span></span><br><span class="line">	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); <span class="comment">//背景</span></span><br><span class="line">	wndclass.lpszClassName = szAppName;                           <span class="comment">//窗口类的名字</span></span><br><span class="line">	wndclass.lpszMenuName = <span class="literal">NULL</span>;                                 <span class="comment">//菜单名字</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//注册窗口，传入窗口类结构指针</span></span><br><span class="line">	<span class="keyword">if</span> (!RegisterClass(&amp;wndclass)) &#123;<span class="comment">//返回NULL</span></span><br><span class="line">		MessageBox(<span class="literal">NULL</span>, TEXT(<span class="string">"这个改程序需要在Windows NT才能执行"</span>), szAppName, MB_ICONERROR);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//创建窗口</span></span><br><span class="line">	hwnd = CreateWindow(szAppName,  <span class="comment">//窗口类名称</span></span><br><span class="line">		TEXT(<span class="string">"中国"</span>),               <span class="comment">//窗口标题</span></span><br><span class="line">		WS_OVERLAPPEDWINDOW,        <span class="comment">//窗口风格</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x方向尺寸</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y方向尺寸</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//父窗口句柄</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//窗口菜单句柄</span></span><br><span class="line">		hInstance,                  <span class="comment">//程序实例句柄</span></span><br><span class="line">		<span class="literal">NULL</span>);                      <span class="comment">//创建参数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示窗口</span></span><br><span class="line">	ShowWindow(hwnd, iCmdShow);</span><br><span class="line">	<span class="comment">//更新窗口</span></span><br><span class="line">	UpdateWindow(hwnd);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//消息循环</span></span><br><span class="line">	<span class="keyword">while</span> (GetMessage(&amp;msg, <span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">0</span>)) &#123;<span class="comment">//WM_QUIT消息，返回0</span></span><br><span class="line">		TranslateMessage(&amp;msg);</span><br><span class="line">		DispatchMessage(&amp;msg);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> msg.wParam;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		GetClientRect(hwnd, &amp;rect);</span><br><span class="line">		DrawText(hdc, TEXT(<span class="string">"大家好，这是我的点第一个窗口程序！"</span>), <span class="number">-1</span>, &amp;rect,</span><br><span class="line">			DT_SINGLELINE | DT_CENTER | DT_VCENTER);</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/20200430162129.png" alt=""></p>
<p>Windows是一个事件驱动，基于消息的操作系统。</p>
<p>事件驱动：</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/20200430175111.png" alt=""></p>
<p>消息机制：</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/20200430175504.png" alt=""></p>
<p>消息机制三点补充：</p>
<ul>
<li><p>消息队列是FIFO的形式，先进先出。</p>
</li>
<li><p>WM_PAINT, WM_TIMER和WM_QUIT这三个消息属于特例，操作系统会把他们时刻放在消息队列的最后。只有等其他消息执行完毕，才能执行它们。</p>
</li>
<li><p>消息其实会细分为队列化消息和非队列化消息。非队列化消息会直接送往窗口过程，会绕过消息队列。</p>
</li>
</ul>
<p>客户区和非客户区：</p>
<p>HDC: Device Context，设备上下文，设备环境，进行绘图绘画都需要HDC。</p>
<p>PAINTSTRUCT：存放绘制窗口所需要的一些参数</p>
<p>RECT：存放矩形的左上角和右下角。</p>
<p>WM_DESTROY消息：在这个消息里面可以处理文件保存等操作，然后调用PostQuitMessage(0)发送WM_QUIT消息来关闭窗口。</p>
<p>响应鼠标左键的消息：</p>
<ul>
<li>客户区鼠标点击响应</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> WM_LBUTTONDOWN:</span><br><span class="line">    MessageBox(hwnd, TEXT(<span class="string">"点我干嘛！"</span>), TEXT(<span class="string">"鼠标1"</span>), MB_OK);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"><span class="keyword">case</span> WM_LBUTTONUP:</span><br><span class="line">    MessageBox(hwnd, TEXT(<span class="string">"点我干嘛！"</span>), TEXT(<span class="string">"鼠标2"</span>), MB_OK);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br></pre></td></tr></table></figure>
<ul>
<li>非客户区鼠标点击响应</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> WM_NCLBUTTONDOWN:</span><br><span class="line">MessageBox(hwnd, TEXT(<span class="string">"在非客户区点我干嘛！"</span>), TEXT(<span class="string">"鼠标"</span>), MB_OK);</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br></pre></td></tr></table></figure>
<p>窗口销毁机制：</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/11111111.jpg" alt=""></p>
<p>点击关闭时，提示是否确认关闭的实现：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> WM_CLOSE:</span><br><span class="line"><span class="keyword">if</span> (MessageBox(hwnd, TEXT(<span class="string">"请问是否真的要关闭窗口"</span>), TEXT(<span class="string">"请确认"</span>), MB_YESNO) == IDYES) &#123;</span><br><span class="line">    DestroyWindow(hwnd);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br></pre></td></tr></table></figure>
<h1 id="文本输出"><a href="#文本输出" class="headerlink" title="文本输出"></a>文本输出</h1><p>设备环境句柄：是应用程序使用GDI函数的通行证</p>
<p>设备环境句柄又称设备上下文，一个普通的数据结构，存放文本的颜色，文本的背景色等信息。</p>
<p>获取设备环境句柄：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//方法一：响应WM_PAINT消息时使用</span></span><br><span class="line">hed = BeginPaint(hwnd, ps);</span><br><span class="line"><span class="comment">//使用GDI函数</span></span><br><span class="line">EndPaint(hwnd, &amp;ps);</span><br><span class="line"></span><br><span class="line"><span class="comment">//方法二：响应非WM_PAINT消息时使用</span></span><br><span class="line">hdc = GetDC(hwnd);</span><br><span class="line"><span class="comment">//使用GDI函数</span></span><br><span class="line">ReleaseDC(hwnd, hdc);</span><br></pre></td></tr></table></figure>
<p>·TextOut：显示文本最重要的函数，可以指定显示的位置。</p>
<p>可以通过GetTextAlign获取当前文本对齐方式，通过调用SetTextAlign修改对齐模式。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">TextOut(</span><br><span class="line">    HDC hdc,</span><br><span class="line">    <span class="keyword">int</span> nxStart,</span><br><span class="line">    <span class="keyword">int</span> nyStart,</span><br><span class="line">    LPCTSTR lpString, <span class="comment">//字符串</span></span><br><span class="line">    <span class="keyword">int</span> cchsting      <span class="comment">//字符串长度</span></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">case</span> WM_PAINT:</span><br><span class="line">    hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">    GetClientRect(hwnd, &amp;rect);</span><br><span class="line">    TextOut(hdc, <span class="number">400</span>, <span class="number">300</span>, TEXT(<span class="string">"大家好，我是某某"</span>),<span class="number">16</span>);</span><br><span class="line">    EndPaint(hwnd, &amp;ps);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br></pre></td></tr></table></figure>
<h1 id="字符串处理"><a href="#字符串处理" class="headerlink" title="字符串处理"></a>字符串处理</h1><p>四个常用的函数：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">wsptrintf</span><br><span class="line"></span><br><span class="line">lstrlen</span><br><span class="line"></span><br><span class="line">lstrcpy</span><br><span class="line"></span><br><span class="line">lstrcat</span><br></pre></td></tr></table></figure>
<p>实例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">TCHAR szBuffer[<span class="number">128</span>];</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> LINEHEIGHT = <span class="number">15</span>;</span><br><span class="line"><span class="keyword">switch</span> (message) &#123;</span><br><span class="line">    <span class="keyword">case</span> WM_PAINT:</span><br><span class="line">    hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">    GetClientRect(hwnd, &amp;rect)</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; ++i)&#123;</span><br><span class="line">    	wsprintf(szBuffer, TEXT(<span class="string">"%d: I love china!"</span>), i+<span class="number">1</span>);</span><br><span class="line">        TextOut(hdc,<span class="number">0</span>, i * LINEHEIGHT, szBuffer, lstrlen(szBuffer));</span><br><span class="line">    &#125;</span><br><span class="line">    EndPaint(hwnd, &amp;ps);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>安全的字符串处理函数：</p>
<p>都包含在<code>#include &lt;strsafe.h&gt;</code>中。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">StringCchPrintf</span><br><span class="line">StringCchLength  </span><br><span class="line">StringCchCopy</span><br><span class="line">StringCchCat</span><br></pre></td></tr></table></figure>
<p>实例：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">TCHAR szBuffer[<span class="number">12</span>];</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> LINEHEIGHT = <span class="number">15</span>;</span><br><span class="line"><span class="keyword">size_t</span> iTarget;</span><br><span class="line"><span class="keyword">switch</span> (message) &#123;</span><br><span class="line">    <span class="keyword">case</span> WM_PAINT:</span><br><span class="line">        hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">        GetClientRect(hwnd, &amp;rect);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; ++i) &#123;</span><br><span class="line">            StringCchPrintf(szBuffer, <span class="number">12</span>, TEXT(<span class="string">"%d: %s"</span>), i + <span class="number">1</span>, TEXT(<span class="string">"I love China"</span>));</span><br><span class="line">            StringCchLength(szBuffer, <span class="number">12</span>, &amp;iTarget);</span><br><span class="line">            TextOut(hdc, <span class="number">0</span>, i * LINEHEIGHT, szBuffer, iTarget);</span><br><span class="line">        &#125;</span><br><span class="line">        EndPaint(hwnd, &amp;ps);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>不要试图猜测文本的大小：</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/20200501001240.png" alt=""></p>
<p>行距：tmHeight+tmExternalLeading</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">HDC hdc;</span><br><span class="line">PAINTSTRUCT  ps;</span><br><span class="line">RECT rect;</span><br><span class="line"></span><br><span class="line">TCHAR szBuffer[<span class="number">12</span>];</span><br><span class="line"><span class="keyword">size_t</span> iTarget;</span><br><span class="line"></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">int</span> cxChar, cyChar;</span><br><span class="line"></span><br><span class="line">TEXTMETRIC tm;</span><br><span class="line"><span class="keyword">switch</span> (message) &#123;</span><br><span class="line"><span class="keyword">case</span> WM_CREATE:</span><br><span class="line">	hdc = GetDC(hwnd);</span><br><span class="line">    GetTextMetrics(hdc, &amp;tm);</span><br><span class="line">    cxChar = tm.tmAveCharWidth;</span><br><span class="line">    cyChar = tm.tmHeight + tm.tmExternalLeading;</span><br><span class="line">    ReleaseDC(hwnd, hdc);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"><span class="keyword">case</span> WM_PAINT:</span><br><span class="line">    hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">    GetClientRect(hwnd, &amp;rect);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; ++i) &#123;</span><br><span class="line">        StringCchPrintf(szBuffer, <span class="number">12</span>, TEXT(<span class="string">"%d: %s"</span>), i + <span class="number">1</span>, TEXT(<span class="string">"I love China"</span>));</span><br><span class="line">    	StringCchLength(szBuffer, <span class="number">12</span>, &amp;iTarget);</span><br><span class="line">        TextOut(hdc, cxChar, i * cyChar, szBuffer, iTarget);</span><br><span class="line">     &#125;</span><br><span class="line">     EndPaint(hwnd, &amp;ps);</span><br><span class="line">     <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="获取窗口的各种尺寸"><a href="#获取窗口的各种尺寸" class="headerlink" title="获取窗口的各种尺寸"></a>获取窗口的各种尺寸</h1><p>GetSystemMetrics函数返回Windows中各种图形的尺寸形式，包括鼠标指针，图标，标题栏和滚动条等等。</p>
<p>参数nIndex参数是一个索引值，</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	TCHAR szBuffer[<span class="number">120</span>];</span><br><span class="line">	<span class="keyword">size_t</span> iTarget;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxClient, cyClient;</span><br><span class="line">	TEXTMETRIC tm;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		cxClient = GetSystemMetrics(SM_CXSCREEN);</span><br><span class="line">		cyClient = GetSystemMetrics(SM_CXSCREEN);</span><br><span class="line">		GetClientRect(hwnd, &amp;rect);</span><br><span class="line">		StringCchPrintf(szBuffer, <span class="number">120</span>, TEXT(<span class="string">"当前显示器分辨率%d * %d px"</span>), cxClient, cyClient);</span><br><span class="line">		StringCchLength(szBuffer, <span class="number">120</span>, &amp;iTarget);</span><br><span class="line">		TextOut(hdc, <span class="number">100</span>, <span class="number">200</span>, szBuffer, iTarget);</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="滚动条"><a href="#滚动条" class="headerlink" title="滚动条"></a>滚动条</h1><h2 id="获取客户区的尺寸"><a href="#获取客户区的尺寸" class="headerlink" title="获取客户区的尺寸"></a>获取客户区的尺寸</h2><ul>
<li>方法一，低级</li>
</ul>
<p>调用GetSystemMetrics(SM_CXFULLSCREEN)，获取窗口最大化时的宽度</p>
<p>调用GetSystemMetrics(SM_CYFULLSCREEN)，获取窗口最大化时的高度</p>
<ul>
<li>方法二，效率太低</li>
</ul>
<p>调用GetClientRect函数来获取当前客户区的大小</p>
<ul>
<li>方法三</li>
</ul>
<p>当主窗口的客户区大小发生变化时，操作系统将给应用程序发送WM_SIZE消息</p>
<p>把客户区的宽度和高度分别存放在<strong>lParam参数的低16位和高16位</strong>，通过下面两个宏定义实现</p>
<p>LOWORD(lParam)</p>
<p>HIWORD(lParam)</p>
<h2 id="添加滚动条"><a href="#添加滚动条" class="headerlink" title="添加滚动条"></a>添加滚动条</h2><p>WS_VSCROLL 垂直滚动条</p>
<p>WS_HSCROLL 水平滚动条</p>
<p>这两个属性添加到CreateWindow函数中的窗口风格中：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//创建窗口</span></span><br><span class="line">hwnd = CreateWindow(szAppName,  <span class="comment">//窗口类名称</span></span><br><span class="line">                    TEXT(<span class="string">"中国"</span>),               <span class="comment">//窗口标题</span></span><br><span class="line">                    WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,        <span class="comment">//窗口风格</span></span><br><span class="line">                    CW_USEDEFAULT,              <span class="comment">//初始x坐标</span></span><br><span class="line">                    CW_USEDEFAULT,              <span class="comment">//初始y坐标</span></span><br><span class="line">                    CW_USEDEFAULT,              <span class="comment">//初始x方向尺寸</span></span><br><span class="line">                    CW_USEDEFAULT,              <span class="comment">//初始y方向尺寸</span></span><br><span class="line">                    <span class="literal">NULL</span>,                       <span class="comment">//父窗口句柄</span></span><br><span class="line">                    <span class="literal">NULL</span>,                       <span class="comment">//窗口菜单句柄</span></span><br><span class="line">                    hInstance,                  <span class="comment">//程序实例句柄</span></span><br><span class="line">                    <span class="literal">NULL</span>);                      <span class="comment">//创建参数</span></span><br></pre></td></tr></table></figure>
<p>添加了上面的代码只能显示滚动条，但是无法拖动滚动跳来实现翻页或滚动一行。</p>
<p>得通过消息来进行响应，具体得由用户实现，具体响应的消息是：</p>
<p>WM_VSCROLL 垂直</p>
<p>WM_HSCROLL 水平</p>
<p>lParam等于0，表示是标准的窗口滚动条，如果该参数等于滚动条窗口句柄，说明它是滚动条控件</p>
<p>当wParam的低16位是SB_THUMBTRACK时，wParam的高16位是用户拖动滚动条滑块的当前位置</p>
<p>当wParam的低16位是SB_LINEDOWN时，表示用户点击了向下翻动一行</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">    </span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_VSCROLL:</span><br><span class="line">		hdc = GetDC(hwnd);</span><br><span class="line">		<span class="keyword">switch</span>(LOWORD(wParam)) </span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">case</span> SB_LINEUP:</span><br><span class="line">				TextOut(hdc, <span class="number">0</span>, <span class="number">0</span>, TEXT(<span class="string">"向上滚动一行..."</span>), <span class="number">9</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">case</span> SB_LINEDOWN:</span><br><span class="line">				TextOut(hdc, <span class="number">0</span>, <span class="number">0</span>, TEXT(<span class="string">"向下滚动一行..."</span>), <span class="number">9</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">case</span> SB_PAGEUP:</span><br><span class="line">				TextOut(hdc, <span class="number">0</span>, <span class="number">0</span>, TEXT(<span class="string">"向下滚动一页..."</span>), <span class="number">9</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">case</span> SB_PAGEDOWN:</span><br><span class="line">				TextOut(hdc, <span class="number">0</span>, <span class="number">0</span>, TEXT(<span class="string">"向下滚动一页..."</span>), <span class="number">9</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">case</span> SB_THUMBTRACK:</span><br><span class="line">				TextOut(hdc, <span class="number">0</span>, <span class="number">0</span>, TEXT(<span class="string">"别抓着我不放..."</span>), <span class="number">9</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		ReleaseDC(hwnd, hdc);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>windows负责：</p>
<ul>
<li>处理滚动条中的所有鼠标消息</li>
<li>当用户点击滚动条时，提供一种反向显示的闪烁</li>
<li>当用户拖动滑块时，在滚动条内移动滑块</li>
<li>向拥有滚动条的窗口的窗口过程发送滚动条消息</li>
</ul>
<p>代码负责：</p>
<ul>
<li>初始化滚动条的范围和位置：SetScrollRange</li>
<li>处理传送给窗口过程的滚动条消息</li>
<li>更新滑块的位置：Set</li>
<li>根据滚动条的变化更新客户区的内容</li>
</ul>
<p>对应的函数：</p>
<ul>
<li>设置滚动条范围：SetScrollRange</li>
<li>设置滚动条位置：SetScrollPos</li>
<li>获取滚动条范围：GetScrollRange</li>
<li>获取滚动条位置：GetScrollPos</li>
</ul>
<p>垂直滚动条设置的流程：</p>
<ul>
<li>在CreateWindow中设置对应的类型风格，来添加滚动条</li>
<li><p>定义一个变量表示滚动条的位置，比如iVScrollPos</p>
</li>
<li><p>在WM_CREATE消息中，初始化滚动条的范围和位置</p>
</li>
<li><p>在WM_VSCROLL消息中，响应不同的滚动条操作，从而设定iVScrollPos的值，并通过SetScrollPos来设定滚动条的位置</p>
</li>
<li><p>最后，通过调用InvalidateRect函数或者UpdateWindow来出发一个WM_PAINT消息，实现重绘窗口的效果，实现客户区内容随滚动条移动。这里面主要就是通过iVScrollPos来进行操作。</p>
<p>SysMets.h</p>
</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> NUMLINES ((int)(sizeof sysMets / sizeof sysMets[0]))</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">	<span class="keyword">int</span> iIndex;</span><br><span class="line">	TCHAR * szContent;</span><br><span class="line">	TCHAR * szDes;</span><br><span class="line">&#125;</span><br><span class="line">sysMets[] = &#123;</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CXSCREEN,    TEXT(<span class="string">"SM_CXSCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Screen width in pixels"</span>),</span><br><span class="line">	SM_CYSCREEN,    TEXT(<span class="string">"SM_CYXCREEN"</span>),</span><br><span class="line">					TEXT(<span class="string">"Sctreen height in pixels"</span>),</span><br><span class="line">	SM_CYVSCROLL,   TEXT(<span class="string">"SM_CYVSCROLL"</span>),</span><br><span class="line">					TEXT(<span class="string">"Vertical scroll width"</span>)</span><br><span class="line"></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>主程序：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;strsafe.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"SysMets.h"</span></span></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND, UINT, WPARAM, LPARAM)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> WINAPI <span class="title">WinMain</span><span class="params">(HINSTANCE hInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	HINSTANCE hPrevInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	PSTR szCmdLine,</span></span></span><br><span class="line"><span class="function"><span class="params">	<span class="keyword">int</span> iCmdShow)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> TCHAR * szAppName = TEXT(<span class="string">"MyWindows"</span>);</span><br><span class="line">	HWND hwnd;            <span class="comment">//句柄，特殊的指针，找到窗口的标识</span></span><br><span class="line">	MSG msg;              <span class="comment">//消息结构，windwos核心就是消息机制</span></span><br><span class="line">	WNDCLASS wndclass;    <span class="comment">//窗口类结构</span></span><br><span class="line"></span><br><span class="line">	wndclass.style = CS_HREDRAW | CS_VREDRAW;                     <span class="comment">//窗口风格, CS_HERDRAW表示水平调整窗口会发生重绘</span></span><br><span class="line">	wndclass.lpfnWndProc = WndProc;                               <span class="comment">//窗口过程</span></span><br><span class="line">	wndclass.cbClsExtra = <span class="number">0</span>;                                      <span class="comment">//预留的额外空间</span></span><br><span class="line">	wndclass.cbWndExtra = <span class="number">0</span>;                                      <span class="comment">//</span></span><br><span class="line">	wndclass.hInstance = hInstance;                               <span class="comment">//实例句柄</span></span><br><span class="line">	wndclass.hIcon = LoadIcon(<span class="literal">NULL</span>, IDI_APPLICATION);             <span class="comment">//图标</span></span><br><span class="line">	wndclass.hCursor = LoadCursor(<span class="literal">NULL</span>, IDC_ARROW);               <span class="comment">//光标</span></span><br><span class="line">	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); <span class="comment">//背景</span></span><br><span class="line">	wndclass.lpszClassName = szAppName;                           <span class="comment">//窗口类的名字</span></span><br><span class="line">	wndclass.lpszMenuName = <span class="literal">NULL</span>;                                 <span class="comment">//菜单名字</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//注册窗口，传入窗口类结构指针</span></span><br><span class="line">	<span class="keyword">if</span> (!RegisterClass(&amp;wndclass)) &#123;<span class="comment">//返回NULL</span></span><br><span class="line">		MessageBox(<span class="literal">NULL</span>, TEXT(<span class="string">"这个改程序需要在Windows NT才能执行"</span>), szAppName, MB_ICONERROR);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//创建窗口</span></span><br><span class="line">	hwnd = CreateWindow(szAppName,  <span class="comment">//窗口类名称</span></span><br><span class="line">		TEXT(<span class="string">"中国"</span>),               <span class="comment">//窗口标题</span></span><br><span class="line">		WS_OVERLAPPEDWINDOW | WS_VSCROLL,        <span class="comment">//窗口风格</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x方向尺寸</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y方向尺寸</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//父窗口句柄</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//窗口菜单句柄</span></span><br><span class="line">		hInstance,                  <span class="comment">//程序实例句柄</span></span><br><span class="line">		<span class="literal">NULL</span>);                      <span class="comment">//创建参数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示窗口</span></span><br><span class="line">	ShowWindow(hwnd, iCmdShow);</span><br><span class="line">	<span class="comment">//更新窗口</span></span><br><span class="line">	UpdateWindow(hwnd);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//消息循环</span></span><br><span class="line">	<span class="keyword">while</span> (GetMessage(&amp;msg, <span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">0</span>)) &#123;<span class="comment">//WM_QUIT消息，返回0</span></span><br><span class="line">		TranslateMessage(&amp;msg);</span><br><span class="line">		DispatchMessage(&amp;msg);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> msg.wParam;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line"></span><br><span class="line">	TCHAR szBuffer[<span class="number">10</span>];</span><br><span class="line">	<span class="keyword">size_t</span> iTarget;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxChar, cyChar, cxCaps;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxClient, cyClient, iVscrollPos;</span><br><span class="line">	TEXTMETRIC tm;</span><br><span class="line">	<span class="keyword">int</span> i, y;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_CREATE:</span><br><span class="line">		hdc = GetDC(hwnd);</span><br><span class="line">		GetTextMetrics(hdc, &amp;tm);</span><br><span class="line">		cxChar = tm.tmAveCharWidth;     <span class="comment">//字符平均宽度</span></span><br><span class="line">		cxCaps = (tm.tmPitchAndFamily &amp; <span class="number">1</span> ? <span class="number">3</span> : <span class="number">2</span>) * cxChar / <span class="number">2</span>; <span class="comment">//如果低位为0,表示等宽字体，如果为1,表示变宽字体，大写字母是小写字母的1.5倍</span></span><br><span class="line">		cyChar = tm.tmHeight + tm.tmExternalLeading;</span><br><span class="line"></span><br><span class="line">		SetScrollRange(hwnd, SB_VERT, <span class="number">0</span>, NUMLINES - <span class="number">1</span>, FALSE);</span><br><span class="line">		SetScrollPos(hwnd, SB_VERT, <span class="number">0</span>, TRUE);</span><br><span class="line">		ReleaseDC(hwnd, hdc);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; NUMLINES; ++i) &#123;</span><br><span class="line">			y = cyChar * (i - iVscrollPos);</span><br><span class="line">			StringCchLength(sysMets[i].szContent, <span class="number">128</span>, &amp;iTarget);</span><br><span class="line">			TextOut(hdc, <span class="number">0</span>, y, sysMets[i].szContent, iTarget);</span><br><span class="line"></span><br><span class="line">			StringCchLength(sysMets[i].szDes, <span class="number">128</span>, &amp;iTarget);</span><br><span class="line">			TextOut(hdc, <span class="number">22</span> * cxCaps, y, sysMets[i].szDes, iTarget);</span><br><span class="line"></span><br><span class="line">			SetTextAlign(hdc, TA_TOP | TA_RIGHT);</span><br><span class="line">			StringCchPrintf(szBuffer, <span class="number">10</span>, TEXT(<span class="string">"%d"</span>), GetSystemMetrics(sysMets[i].iIndex));</span><br><span class="line">			StringCchLength(szBuffer, <span class="number">10</span>, &amp;iTarget);</span><br><span class="line">			TextOut(hdc, <span class="number">22</span> * cxCaps + <span class="number">40</span> * cxChar, y, szBuffer, iTarget);</span><br><span class="line"></span><br><span class="line">			SetTextAlign(hdc, TA_TOP | TA_LEFT);</span><br><span class="line"></span><br><span class="line">		&#125;</span><br><span class="line">		EndPaint(hwnd, &amp;hdc);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_VSCROLL:</span><br><span class="line">		hdc = GetDC(hwnd);</span><br><span class="line">		<span class="keyword">switch</span> (LOWORD(wParam))</span><br><span class="line">		&#123;</span><br><span class="line">		<span class="keyword">case</span> SB_LINEUP:</span><br><span class="line">			iVscrollPos -= <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_LINEDOWN:</span><br><span class="line">			<span class="comment">//TextOut(hdc, 0, 0, TEXT("向下滚动一行..."), 9);</span></span><br><span class="line">			iVscrollPos += <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_PAGEUP:</span><br><span class="line">			iVscrollPos -= (<span class="keyword">int</span>)(cyClient / cyChar);</span><br><span class="line">			<span class="comment">//TextOut(hdc, 0, 0, TEXT("向下滚动一页..."), 9);</span></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_PAGEDOWN:</span><br><span class="line">			iVscrollPos += (<span class="keyword">int</span>)(cyClient / cyChar);</span><br><span class="line">			<span class="comment">//TextOut(hdc, 0, 0, TEXT("向下滚动一页..."), 9);</span></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_THUMBPOSITION:</span><br><span class="line">			iVscrollPos = HIWORD(wParam);</span><br><span class="line">			<span class="comment">//TextOut(hdc, 0, 0, TEXT("别抓着我不放..."), 9);</span></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		iVscrollPos = max(<span class="number">0</span>, min(iVscrollPos, NUMLINES - <span class="number">1</span>));</span><br><span class="line">		ReleaseDC(hwnd, hdc);</span><br><span class="line">		<span class="keyword">if</span> (iVscrollPos != GetScrollPos(hwnd, SB_VERT)) &#123;</span><br><span class="line">			SetScrollPos(hwnd, SB_VERT, iVscrollPos, TRUE);</span><br><span class="line">			InvalidateRect(hwnd, <span class="literal">NULL</span>, TRUE);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上述实现的问题：</p>
<ul>
<li>效率低，每次移动一下滚动条需要重绘窗口</li>
<li>实际上所有内容被打印出来，只是在客户区外没有被显示</li>
</ul>
<p><strong>优化版本：</strong></p>
<p>放弃使用之前的四个函数<code>SetScrollPos</code>等。而使用下面两个函数：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">SetScrollInfo(HWND hwnd, <span class="keyword">int</span> fnBar, LPCSCROLLINFO lpsi, BOLL fRedraw)</span><br><span class="line">GetScrollInfo()</span><br></pre></td></tr></table></figure>
<p>SCROLLINFO结构体：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">cbSize    <span class="comment">//该结构体大小</span></span><br><span class="line">fMask     <span class="comment">//指定设置或获取滚动条参数，该参数由标志组成</span></span><br><span class="line">nMin      <span class="comment">//滚动条范围的最小值</span></span><br><span class="line">nMax      <span class="comment">//滚动条范围的最大值</span></span><br><span class="line">nPage     <span class="comment">//页面的大小</span></span><br><span class="line">nPos      <span class="comment">//滑块的位置</span></span><br><span class="line">nTrackPos <span class="comment">//当前滑块的位置</span></span><br></pre></td></tr></table></figure>
<p>ScrollWindow，ScrollWindowEx可以让客户区的内容滚动起来。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">Bool <span class="title">ScrollWindow</span><span class="params">(HWND hwnd, </span></span></span><br><span class="line"><span class="function"><span class="params">                 <span class="keyword">int</span> XAmount,</span></span></span><br><span class="line"><span class="function"><span class="params">                 <span class="keyword">int</span> YAmount,</span></span></span><br><span class="line"><span class="function"><span class="params">                 <span class="keyword">const</span> RECT * lpRect,</span></span></span><br><span class="line"><span class="function"><span class="params">                 <span class="keyword">const</span> RECT * lpClipRect)</span></span>;</span><br></pre></td></tr></table></figure>
<p>ScrollWindow只滚动已显示的内容，下方的内容会变成失效区域，只要重绘失效区域就行。通过WM_PAINT来实现失效区域的重绘。</p>
<p>具体实现流程：</p>
<ul>
<li>WM_CREATE消息中获取字体的宽度和高度</li>
<li>WM_SIZE消息中，设置滚动条的范围和页面大小</li>
<li>WM_VSCRLL消息中，响应垂直滚动条的操作，并设置滚动条的位置，以及滚动客户区中的内容，并发送WM_PAINT消息以实现重绘失效区域。</li>
<li>WM_PAINT消息中，重绘失效区域</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;strsafe.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"SysMets.h"</span></span></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND, UINT, WPARAM, LPARAM)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> WINAPI <span class="title">WinMain</span><span class="params">(HINSTANCE hInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	HINSTANCE hPrevInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	PSTR szCmdLine,</span></span></span><br><span class="line"><span class="function"><span class="params">	<span class="keyword">int</span> iCmdShow)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> TCHAR * szAppName = TEXT(<span class="string">"MyWindows"</span>);</span><br><span class="line">	HWND hwnd;            <span class="comment">//句柄，特殊的指针，找到窗口的标识</span></span><br><span class="line">	MSG msg;              <span class="comment">//消息结构，windwos核心就是消息机制</span></span><br><span class="line">	WNDCLASS wndclass;    <span class="comment">//窗口类结构</span></span><br><span class="line"></span><br><span class="line">	wndclass.style = CS_HREDRAW | CS_VREDRAW;                     <span class="comment">//窗口风格, CS_HERDRAW表示水平调整窗口会发生重绘</span></span><br><span class="line">	wndclass.lpfnWndProc = WndProc;                               <span class="comment">//窗口过程</span></span><br><span class="line">	wndclass.cbClsExtra = <span class="number">0</span>;                                      <span class="comment">//预留的额外空间</span></span><br><span class="line">	wndclass.cbWndExtra = <span class="number">0</span>;                                      <span class="comment">//</span></span><br><span class="line">	wndclass.hInstance = hInstance;                               <span class="comment">//实例句柄</span></span><br><span class="line">	wndclass.hIcon = LoadIcon(<span class="literal">NULL</span>, IDI_APPLICATION);             <span class="comment">//图标</span></span><br><span class="line">	wndclass.hCursor = LoadCursor(<span class="literal">NULL</span>, IDC_ARROW);               <span class="comment">//光标</span></span><br><span class="line">	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); <span class="comment">//背景</span></span><br><span class="line">	wndclass.lpszClassName = szAppName;                           <span class="comment">//窗口类的名字</span></span><br><span class="line">	wndclass.lpszMenuName = <span class="literal">NULL</span>;                                 <span class="comment">//菜单名字</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//注册窗口，传入窗口类结构指针</span></span><br><span class="line">	<span class="keyword">if</span> (!RegisterClass(&amp;wndclass)) &#123;<span class="comment">//返回NULL</span></span><br><span class="line">		MessageBox(<span class="literal">NULL</span>, TEXT(<span class="string">"这个改程序需要在Windows NT才能执行"</span>), szAppName, MB_ICONERROR);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//创建窗口</span></span><br><span class="line">	hwnd = CreateWindow(szAppName,  <span class="comment">//窗口类名称</span></span><br><span class="line">		TEXT(<span class="string">"中国"</span>),               <span class="comment">//窗口标题</span></span><br><span class="line">		WS_OVERLAPPEDWINDOW | WS_VSCROLL,        <span class="comment">//窗口风格</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x方向尺寸</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y方向尺寸</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//父窗口句柄</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//窗口菜单句柄</span></span><br><span class="line">		hInstance,                  <span class="comment">//程序实例句柄</span></span><br><span class="line">		<span class="literal">NULL</span>);                      <span class="comment">//创建参数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示窗口</span></span><br><span class="line">	ShowWindow(hwnd, iCmdShow);</span><br><span class="line">	<span class="comment">//更新窗口</span></span><br><span class="line">	UpdateWindow(hwnd);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//消息循环</span></span><br><span class="line">	<span class="keyword">while</span> (GetMessage(&amp;msg, <span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">0</span>)) &#123;<span class="comment">//WM_QUIT消息，返回0</span></span><br><span class="line">		TranslateMessage(&amp;msg);</span><br><span class="line">		DispatchMessage(&amp;msg);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> msg.wParam;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line"></span><br><span class="line">	TCHAR szBuffer[<span class="number">10</span>];</span><br><span class="line">	<span class="keyword">size_t</span> iTarget;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxChar, cyChar, cxCaps;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxClient, cyClient, iVscrollPos;</span><br><span class="line">	TEXTMETRIC tm;</span><br><span class="line">	<span class="keyword">int</span> i, y;</span><br><span class="line"></span><br><span class="line">	SCROLLINFO si;</span><br><span class="line">	<span class="keyword">int</span> yPos, FirstLine, LastLine;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_CREATE:</span><br><span class="line">		hdc = GetDC(hwnd);</span><br><span class="line">		GetTextMetrics(hdc, &amp;tm);</span><br><span class="line">		cxChar = tm.tmAveCharWidth;     <span class="comment">//字符平均宽度</span></span><br><span class="line">		cxCaps = (tm.tmPitchAndFamily &amp; <span class="number">1</span> ? <span class="number">3</span> : <span class="number">2</span>) * cxChar / <span class="number">2</span>; <span class="comment">//如果低位为0,表示等宽字体，如果为1,表示变宽字体，大写字母是小写字母的1.5倍</span></span><br><span class="line">		cyChar = tm.tmHeight + tm.tmExternalLeading;</span><br><span class="line"></span><br><span class="line">		ReleaseDC(hwnd, hdc);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		hdc = GetDC(hwnd);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//设置滚动条，包括范围和页面大小</span></span><br><span class="line">		si.cbSize = <span class="keyword">sizeof</span>(si);</span><br><span class="line">		si.fMask = SIF_RANGE | SIF_PAGE;</span><br><span class="line">		si.nMax = NUMLINES<span class="number">-1</span>;</span><br><span class="line">		si.nMin = <span class="number">0</span>;</span><br><span class="line">		si.nPage = cyClient / cyChar;</span><br><span class="line">		SetScrollInfo(hwnd, SB_VERT, &amp;si, TRUE);</span><br><span class="line"></span><br><span class="line">		ReleaseDC(hwnd, hdc);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取滚动条位置</span></span><br><span class="line">		si.cbSize = <span class="keyword">sizeof</span>(si);</span><br><span class="line">		si.fMask = SIF_POS;</span><br><span class="line">		GetScrollInfo(hwnd, SB_VERT, &amp;si);</span><br><span class="line">		yPos = si.nPos;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//计算需要重绘的区域</span></span><br><span class="line">		FirstLine = max(<span class="number">0</span>, yPos + ps.rcPaint.top / cyChar);</span><br><span class="line">		LastLine = min(NUMLINES<span class="number">-1</span>, yPos + ps.rcPaint.bottom / cyChar);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//重绘失效区域</span></span><br><span class="line">		<span class="keyword">for</span> (i = FirstLine; i &lt;= LastLine; ++i) &#123;</span><br><span class="line">			y = cyChar * (i - yPos);</span><br><span class="line">			StringCchLength(sysMets[i].szContent, <span class="number">128</span>, &amp;iTarget);</span><br><span class="line">			TextOut(hdc, <span class="number">0</span>, y, sysMets[i].szContent, iTarget);</span><br><span class="line"></span><br><span class="line">			StringCchLength(sysMets[i].szDes, <span class="number">128</span>, &amp;iTarget);</span><br><span class="line">			TextOut(hdc, <span class="number">22</span> * cxCaps, y, sysMets[i].szDes, iTarget);</span><br><span class="line"></span><br><span class="line">			SetTextAlign(hdc, TA_TOP | TA_RIGHT);</span><br><span class="line">			StringCchPrintf(szBuffer, <span class="number">10</span>, TEXT(<span class="string">"%d"</span>), GetSystemMetrics(sysMets[i].iIndex));</span><br><span class="line">			StringCchLength(szBuffer, <span class="number">10</span>, &amp;iTarget);</span><br><span class="line">			TextOut(hdc, <span class="number">22</span> * cxCaps + <span class="number">40</span> * cxChar, y, szBuffer, iTarget);</span><br><span class="line"></span><br><span class="line">			SetTextAlign(hdc, TA_TOP | TA_LEFT);</span><br><span class="line"></span><br><span class="line">		&#125;</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_VSCROLL:</span><br><span class="line">		<span class="comment">//获取滚动条的位置</span></span><br><span class="line">		si.cbSize = <span class="keyword">sizeof</span>(si);</span><br><span class="line">		si.fMask = SIF_ALL;</span><br><span class="line">		GetScrollInfo(hwnd, SB_VERT, &amp;si);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//记录当前滚动条位置，以为了后续判断是否滚动条位置发生变化</span></span><br><span class="line">		yPos = si.nPos;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//响应各种操作</span></span><br><span class="line">		<span class="keyword">switch</span> (LOWORD(wParam))</span><br><span class="line">		&#123;</span><br><span class="line">		<span class="keyword">case</span> SB_LINEUP:</span><br><span class="line">			si.nPos -= <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_LINEDOWN:</span><br><span class="line">			si.nPos += <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_PAGEUP:</span><br><span class="line">			si.nPos -= si.nPage;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_PAGEDOWN:</span><br><span class="line">			si.nPos += si.nPage;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_THUMBTRACK:</span><br><span class="line">			si.nPos = si.nTrackPos;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//设置滚动条位置</span></span><br><span class="line">		si.fMask = SIF_POS;</span><br><span class="line">		SetScrollInfo(hwnd, SB_VERT, &amp;si, TRUE);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//重新获取滚动条位置</span></span><br><span class="line">		GetScrollInfo(hwnd, SB_VERT, &amp;si);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//判断滚动条位置是否发生变化</span></span><br><span class="line">		<span class="keyword">if</span> (yPos != si.nPos) &#123;</span><br><span class="line">			<span class="comment">//滚动客户区的内容</span></span><br><span class="line">			ScrollWindow(hwnd, <span class="number">0</span>, cyChar * (yPos - si.nPos), <span class="literal">NULL</span>, <span class="literal">NULL</span>);</span><br><span class="line">			<span class="comment">//发生WM_PAINT消息，重新绘制失效区域</span></span><br><span class="line">			UpdateWindow(hwnd);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="窗口绘画"><a href="#窗口绘画" class="headerlink" title="窗口绘画"></a>窗口绘画</h1><p>SetPixel：绘制一个车像素点为指定颜色</p>
<p>GetPixel：获取一个像素点的颜色</p>
<p>SetPixelV：比SetPixel快</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">SetPixle(HWND hwnd, <span class="keyword">int</span> x, <span class="keyword">int</span> y, COLORREF crColor);</span><br><span class="line"><span class="comment">//crColor是一个RGB</span></span><br></pre></td></tr></table></figure>
<h2 id="画直线"><a href="#画直线" class="headerlink" title="画直线"></a>画直线</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">int</span> i;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		GetClientRect(hwnd, &amp;rect);</span><br><span class="line">		<span class="keyword">for</span> (i = rect.left; i &lt; rect.right; ++i)</span><br><span class="line">			SetPixel(hdc, i, <span class="number">100</span>, RGB(<span class="number">255</span>, <span class="number">0</span>, <span class="number">0</span>));	</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>MoveToEx：指定直线的起点</p>
<p>LineTo：指定直线的终点</p>
<p>GetCurrentPositionEx：获取当前位置的坐标</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">BOOL <span class="title">MoveToEx</span><span class="params">(HDC hdc, </span></span></span><br><span class="line"><span class="function"><span class="params">             <span class="keyword">int</span> X,</span></span></span><br><span class="line"><span class="function"><span class="params">             <span class="keyword">int</span> Y,</span></span></span><br><span class="line"><span class="function"><span class="params">             LPPOINT lpPoint)</span></span>; <span class="comment">//lpPoint之前坐标，NULL表示不返回</span></span><br><span class="line"></span><br><span class="line"><span class="function">BOOL <span class="title">LineTo</span><span class="params">(HDC hdc,</span></span></span><br><span class="line"><span class="function"><span class="params">           <span class="keyword">int</span> nXEnd,</span></span></span><br><span class="line"><span class="function"><span class="params">           <span class="keyword">int</span> nYEnd)</span></span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">case</span> WM_PAINT:</span><br><span class="line">    hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">    MoveToEx(hdc, <span class="number">100</span>, <span class="number">100</span>, <span class="literal">NULL</span>); <span class="comment">//如果不指定起点，则默认是左上角</span></span><br><span class="line">    LineTo(hdc, <span class="number">600</span>, <span class="number">100</span>);</span><br><span class="line">    EndPaint(hwnd, &amp;ps);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br></pre></td></tr></table></figure>
<p>练习题：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">int</span> i;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//网格实现</span></span><br><span class="line">		GetClientRect(hwnd, &amp;rect);</span><br><span class="line">		<span class="keyword">for</span> (i = rect.top; i &lt; rect.bottom; i += <span class="number">50</span>) &#123;</span><br><span class="line">			 MoveToEx(hdc, rect.left, i, <span class="literal">NULL</span>);</span><br><span class="line">			 LineTo(hdc, rect.right, i);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">for</span> (i = rect.left; i &lt; rect.right; i += <span class="number">50</span>) &#123;</span><br><span class="line">			MoveToEx(hdc, i, rect.top, <span class="literal">NULL</span>);</span><br><span class="line">			LineTo(hdc, i, rect.bottom);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">		<span class="comment">//五角星</span></span><br><span class="line">		MoveToEx(hdc, <span class="number">30</span>, <span class="number">10</span>, <span class="literal">NULL</span>);</span><br><span class="line">		LineTo(hdc, <span class="number">20</span>, <span class="number">50</span>);</span><br><span class="line">		LineTo(hdc, <span class="number">50</span>, <span class="number">20</span>);</span><br><span class="line">		LineTo(hdc, <span class="number">10</span>, <span class="number">20</span>);</span><br><span class="line">		LineTo(hdc, <span class="number">40</span>, <span class="number">50</span>);</span><br><span class="line">		LineTo(hdc, <span class="number">30</span>, <span class="number">10</span>);</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="折线"><a href="#折线" class="headerlink" title="折线"></a>折线</h2><p>Polyline：画一条折线</p>
<p>PolylineTo：可以修改当前位置，画折线时从起点开始，其他跟Polyline相同。</p>
<p>PolyPolyline：同时画多条折线</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">POINT apt[] = &#123; <span class="number">30</span>, <span class="number">10</span>, <span class="number">20</span>, <span class="number">50</span>, <span class="number">50</span>, <span class="number">20</span>, <span class="number">10</span>, <span class="number">20</span>, <span class="number">40</span>, <span class="number">50</span>, <span class="number">30</span>, <span class="number">10</span> &#125;;</span><br><span class="line">Polyline(hdc, apt, <span class="number">6</span>);</span><br><span class="line">PolylineTo(hdc, apt, <span class="number">6</span>);</span><br><span class="line"></span><br><span class="line">POINT apt[] = &#123; <span class="number">50</span>, <span class="number">20</span>, <span class="number">20</span>, <span class="number">60</span>, <span class="number">80</span>, <span class="number">60</span>, <span class="number">50</span>, <span class="number">20</span>, <span class="number">70</span>, <span class="number">20</span>, <span class="number">100</span>, <span class="number">60</span>, <span class="number">130</span>, <span class="number">20</span>, <span class="number">70</span>,  <span class="number">20</span>, <span class="number">150</span>, <span class="number">20</span>, <span class="number">150</span>, <span class="number">60</span>, <span class="number">190</span>, <span class="number">60</span>, <span class="number">190</span>, <span class="number">20</span>, <span class="number">150</span>, <span class="number">20</span>&#125;;</span><br><span class="line"><span class="keyword">int</span> asz[] = &#123; <span class="number">4</span>,<span class="number">4</span>,<span class="number">5</span> &#125;;</span><br><span class="line">PolyPolyline(hdc, apt, asz, <span class="number">3</span>);</span><br></pre></td></tr></table></figure>
<p>sin(x)绘制：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> NUM 1000</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> TWOPI (2 * 3.14159)</span></span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">int</span> cxClient, cyClient;</span><br><span class="line">	<span class="keyword">int</span> i;</span><br><span class="line">	POINT apt[NUM];</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		cxClient = LOWORD(lParam);</span><br><span class="line">		cyClient = HIWORD(lParam);</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line"></span><br><span class="line">		MoveToEx(hdc, <span class="number">0</span>, cyClient / <span class="number">2</span>, <span class="literal">NULL</span>);</span><br><span class="line">		LineTo(hdc, cxClient, cyClient / <span class="number">2</span>);</span><br><span class="line">		<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; NUM; ++i) &#123;</span><br><span class="line">			apt[i].x = cxClient * i / NUM;</span><br><span class="line">			apt[i].y = (<span class="keyword">int</span>)(cyClient / <span class="number">2</span> * (<span class="number">1</span> - <span class="built_in">sin</span>(TWOPI * i / NUM)));</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		PolylineTo(hdc, apt, NUM);</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>贝塞尔曲线：</p>
<p>PolyBezier：不会使用画笔的当前位置，并不会改变最后画笔的位置</p>
<p>PolyBezierTo：使用画笔的当前位置，并改变最后画笔的位置</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxClient, cyClient;</span><br><span class="line">	<span class="keyword">int</span> i;</span><br><span class="line">	<span class="keyword">static</span> POINT apt[<span class="number">4</span>];</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		cxClient = LOWORD(lParam);</span><br><span class="line">		cyClient = HIWORD(lParam);</span><br><span class="line">		apt[<span class="number">0</span>].x = cxClient / <span class="number">4</span>;</span><br><span class="line">		apt[<span class="number">0</span>].y = cyClient / <span class="number">2</span>;</span><br><span class="line">		apt[<span class="number">1</span>].x = cxClient / <span class="number">2</span>;</span><br><span class="line">		apt[<span class="number">1</span>].y = cyClient / <span class="number">4</span>;</span><br><span class="line">		apt[<span class="number">2</span>].x = cxClient / <span class="number">2</span>;</span><br><span class="line">		apt[<span class="number">2</span>].y = cyClient * <span class="number">3</span> / <span class="number">4</span>;</span><br><span class="line">		apt[<span class="number">3</span>].x = cxClient * <span class="number">3</span> / <span class="number">4</span>;</span><br><span class="line">		apt[<span class="number">3</span>].y = cyClient / <span class="number">2</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		PolyBezier(hdc, apt, <span class="number">4</span>);</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>边框绘制函数：</p>
<p>Rectangle</p>
<p>Ellipse</p>
<p>RoundRect</p>
<p>Arc</p>
<p>Chord</p>
<p>Pie</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> WM_PAINT:</span><br><span class="line">    hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">    <span class="comment">//PolyBezier(hdc, apt, 4);</span></span><br><span class="line">    Rectangle(hdc, <span class="number">100</span>, <span class="number">100</span>, <span class="number">300</span>, <span class="number">300</span>);</span><br><span class="line">    Rectangle(hdc, <span class="number">150</span>, <span class="number">50</span>, <span class="number">200</span>, <span class="number">300</span>);</span><br><span class="line">    Ellipse(hdc, <span class="number">200</span>, <span class="number">200</span>, <span class="number">300</span>, <span class="number">400</span>);</span><br><span class="line">    RoundRect(hdc, <span class="number">400</span>, <span class="number">400</span>, <span class="number">500</span>, <span class="number">600</span>, <span class="number">20</span>, <span class="number">20</span>);</span><br><span class="line">    <span class="comment">//Arc(hdc, 100, 300, 500, 500, 300,300, 50, 500);</span></span><br><span class="line">    <span class="comment">//Chord(hdc, 100, 300, 500, 500, 300, 300, 50, 500);</span></span><br><span class="line">    Pie(hdc, <span class="number">100</span>, <span class="number">300</span>, <span class="number">500</span>, <span class="number">500</span>, <span class="number">300</span>, <span class="number">300</span>, <span class="number">50</span>, <span class="number">500</span>);</span><br><span class="line"></span><br><span class="line">    EndPaint(hwnd, &amp;ps);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br></pre></td></tr></table></figure>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">case</span> WM_PAINT:</span><br><span class="line">    hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">    Rectangle(hdc, cxClient / <span class="number">8</span>, cyClient / <span class="number">8</span>, cxClient * <span class="number">7</span> / <span class="number">8</span>, cyClient * <span class="number">7</span> / <span class="number">8</span>);</span><br><span class="line">    MoveToEx(hdc, <span class="number">0</span>, <span class="number">0</span>, <span class="literal">NULL</span>);</span><br><span class="line">    LineTo(hdc, cxClient, cyClient);</span><br><span class="line">    MoveToEx(hdc, cxClient, <span class="number">0</span>, <span class="literal">NULL</span>);</span><br><span class="line">    LineTo(hdc, <span class="number">0</span>, cyClient);</span><br><span class="line">    Ellipse(hdc, cxClient / <span class="number">8</span>, cyClient / <span class="number">8</span>, cxClient * <span class="number">7</span> / <span class="number">8</span>, cyClient * <span class="number">7</span> / <span class="number">8</span>);</span><br><span class="line">    RoundRect(hdc, cxClient / <span class="number">4</span>, cyClient / <span class="number">4</span>, cxClient * <span class="number">3</span> / <span class="number">4</span>, cyClient * <span class="number">3</span> / <span class="number">4</span>, cxClient / <span class="number">4</span>, cyClient / <span class="number">4</span>);</span><br><span class="line"></span><br><span class="line">    EndPaint(hwnd, &amp;ps);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br></pre></td></tr></table></figure>
<h1 id="画笔"><a href="#画笔" class="headerlink" title="画笔"></a>画笔</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">HGDIOBJ <span class="title">SelectObject</span><span class="params">(HDC hdc,</span></span></span><br><span class="line"><span class="function"><span class="params">HGDIOBJ hgiobj</span></span></span><br><span class="line"><span class="function"><span class="params">)</span></span>;                           <span class="comment">//选择画笔放入设备环境中</span></span><br><span class="line"></span><br><span class="line"><span class="function">HGDIOBJ <span class="title">GetStockObject</span><span class="params">(<span class="keyword">int</span> fnObject)</span></span>; <span class="comment">//获取画笔</span></span><br><span class="line"></span><br><span class="line"><span class="function">HPEN <span class="title">CreatePen</span><span class="params">(<span class="keyword">int</span> fnPenStyle,</span></span></span><br><span class="line"><span class="function"><span class="params">              <span class="keyword">int</span> nWidth,</span></span></span><br><span class="line"><span class="function"><span class="params">              COLORREF crColor)</span></span>;   <span class="comment">//创建画笔</span></span><br><span class="line"></span><br><span class="line"><span class="function">HPEN <span class="title">CreatePenIndirect</span><span class="params">()</span></span>;<span class="comment">//使用LOGPEN结构，一般用于批量创建多个画笔，这样就可以创建一个LOGPEN结构体数组，然后用循环去创建   </span></span><br><span class="line"></span><br><span class="line"><span class="function">BOOL <span class="title">DeleteObject</span><span class="params">(HGDIOBJ hObject)</span></span>; <span class="comment">//用于删除一个逻辑画笔，画刷，字体，位图，区域或者调色板，注意系统的画笔无法删除</span></span><br><span class="line"></span><br><span class="line">SetBkMode(hdc, TRANSPARENT);    <span class="comment">//设置背景模式，TRANSPARENT表示透明</span></span><br><span class="line"></span><br><span class="line">SetBkColor(hdc, RGB(<span class="number">0</span>, <span class="number">255</span>, <span class="number">0</span>)); <span class="comment">//设置画笔的背景颜色，此时应该是非透明的</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">二元光栅操作：</span></span><br><span class="line"><span class="comment">SetOp2</span></span><br><span class="line"><span class="comment">GetOp2</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line">SetROP2(hdc, R2_NOT);        <span class="comment">//设置画笔颜色与客户区背景颜色相反</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">GetROP2</span><span class="params">(HDC hdc)</span></span>;         <span class="comment">//获取设备环境的前景色混合模式</span></span><br></pre></td></tr></table></figure>
<p>实例：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	HPEN hPen, hOldPen;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		GetClientRect(hwnd, &amp;rect);</span><br><span class="line">		<span class="comment">//hPen = GetStockObject(BLACK_PEN);</span></span><br><span class="line">		hPen = CreatePen(PS_SOLID, <span class="number">30</span>, RGB(<span class="number">100</span>, <span class="number">0</span>, <span class="number">0</span>)); </span><br><span class="line">		<span class="comment">//hPen = CreatePen(PS_DOT, 2, RGB(255, 0, 0)); //画虚线时，必须小于等于1,</span></span><br><span class="line">		hOldPen = SelectObject(hdc, hPen);</span><br><span class="line">		Ellipse(hdc, <span class="number">100</span>, <span class="number">100</span>, <span class="number">300</span>, <span class="number">300</span>);</span><br><span class="line">		hPen = CreatePen(PS_INSIDEFRAME, <span class="number">30</span>, RGB(<span class="number">255</span>, <span class="number">0</span>, <span class="number">0</span>));  <span class="comment">//内部扩展</span></span><br><span class="line">		SelectObject(hdc, hPen);</span><br><span class="line">		Ellipse(hdc, <span class="number">400</span>, <span class="number">100</span>, <span class="number">500</span>, <span class="number">300</span>); </span><br><span class="line"></span><br><span class="line">		SelectObject(hdc, hOldPen);</span><br><span class="line"></span><br><span class="line">		MoveToEx(hdc, <span class="number">0</span>, <span class="number">100</span>, <span class="literal">NULL</span>);</span><br><span class="line">		LineTo(hdc, rect.right, <span class="number">100</span>);</span><br><span class="line"></span><br><span class="line">		MoveToEx(hdc, <span class="number">0</span>, <span class="number">300</span>, <span class="literal">NULL</span>);</span><br><span class="line">		LineTo(hdc, rect.right, <span class="number">300</span>);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//MoveToEx(hdc, rect.left, (rect.bottom - rect.top) / 2, NULL);</span></span><br><span class="line">		<span class="comment">//LineTo(hdc, rect.right, (rect.bottom - rect.top) / 2);</span></span><br><span class="line">		SelectObject(hwnd, hOldPen);</span><br><span class="line">		EndPaint(hdc, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="画刷"><a href="#画刷" class="headerlink" title="画刷"></a>画刷</h1><p>画刷不是一个一个像素填充的，而是8*8像素的位图来填充的，效率更高。</p>
<p>画刷：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">CreateSolidBrush       <span class="comment">//创建实心画刷</span></span><br><span class="line">CreateHatchBrush</span><br><span class="line">CreateBrushIndirect    <span class="comment">//其参数为LOGBRUSH的结构体，用于批量初始化画刷</span></span><br></pre></td></tr></table></figure>
<p>绘制填充区域</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">static</span> HBRUSH hBrush, hOldBrush;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		GetClientRect(hwnd, &amp;rect);</span><br><span class="line">		TextOut(hdc, (rect.right - rect.left) / <span class="number">2</span>, (rect.bottom - rect.top) / <span class="number">2</span>, TEXT(<span class="string">"I love china"</span>), <span class="number">12</span>);</span><br><span class="line">		<span class="comment">//hBrush = GetStockObject(NULL_BRUSH);</span></span><br><span class="line">		<span class="comment">//hBrush = GetStockObject(LTGRAY_BRUSH);</span></span><br><span class="line">		hBrush = CreateSolidBrush(RGB(<span class="number">255</span>, <span class="number">255</span>, <span class="number">0</span>)); <span class="comment">//实心画刷//需要DeleteObject(hBrush)</span></span><br><span class="line">		</span><br><span class="line">		hOldBrush = SelectObject(hdc, hBrush);</span><br><span class="line">		Ellipse(hdc, (rect.right - rect.left) / <span class="number">4</span>, (rect.bottom - rect.top) / <span class="number">4</span>,</span><br><span class="line">			(rect.right - rect.left) * <span class="number">3</span> / <span class="number">4</span>, (rect.bottom - rect.top) * <span class="number">3</span> / <span class="number">4</span>);</span><br><span class="line">		SelectObject(hdc, hOldBrush);</span><br><span class="line">		EndPaint(hdc, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>CreateHatchBrush的六种不同阴影：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">static</span> HBRUSH hBrush, hOldBrush;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		GetClientRect(hwnd, &amp;rect);</span><br><span class="line">				</span><br><span class="line">		SetBkColor(hdc, RGB(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>)); <span class="comment">//设置画刷背景为黑色 </span></span><br><span class="line"></span><br><span class="line">		hBrush = CreateHatchBrush(HS_BDIAGONAL, RGB(<span class="number">255</span>, <span class="number">255</span>, <span class="number">0</span>));</span><br><span class="line">		hOldBrush = SelectObject(hdc, hBrush);</span><br><span class="line">		Rectangle(hdc, (rect.right - rect.left) /<span class="number">7</span>, (rect.bottom - rect.top) / <span class="number">5</span>,</span><br><span class="line">			(rect.right - rect.left) * <span class="number">2</span> / <span class="number">7</span>, (rect.bottom - rect.top) * <span class="number">2</span> / <span class="number">5</span>);</span><br><span class="line">		DeleteObject(SelectObject(hdc, hOldBrush));</span><br><span class="line"></span><br><span class="line">		hBrush = CreateHatchBrush(HS_CROSS, RGB(<span class="number">255</span>, <span class="number">0</span>, <span class="number">255</span>));</span><br><span class="line">		hOldBrush = SelectObject(hdc, hBrush);</span><br><span class="line">		Rectangle(hdc, (rect.right - rect.left) * <span class="number">3</span> / <span class="number">7</span>, (rect.bottom - rect.top) / <span class="number">5</span>,</span><br><span class="line">			(rect.right - rect.left) * <span class="number">4</span> / <span class="number">7</span>, (rect.bottom - rect.top) * <span class="number">2</span> / <span class="number">5</span>);</span><br><span class="line">		DeleteObject(SelectObject(hdc, hOldBrush));</span><br><span class="line"></span><br><span class="line">		hBrush = CreateHatchBrush(HS_DIAGCROSS, RGB(<span class="number">255</span>, <span class="number">0</span>, <span class="number">0</span>));</span><br><span class="line">		hOldBrush = SelectObject(hdc, hBrush);</span><br><span class="line">		Rectangle(hdc, (rect.right - rect.left) * <span class="number">5</span> / <span class="number">7</span>, (rect.bottom - rect.top) / <span class="number">5</span>,</span><br><span class="line">			(rect.right - rect.left) * <span class="number">6</span> / <span class="number">7</span>, (rect.bottom - rect.top) * <span class="number">2</span> / <span class="number">5</span>);</span><br><span class="line">		DeleteObject(SelectObject(hdc, hOldBrush));</span><br><span class="line"></span><br><span class="line">		hBrush = CreateHatchBrush(HS_FDIAGONAL, RGB(<span class="number">0</span>, <span class="number">0</span>, <span class="number">255</span>));</span><br><span class="line">		hOldBrush = SelectObject(hdc, hBrush);</span><br><span class="line">		Rectangle(hdc, (rect.right - rect.left) / <span class="number">7</span>, (rect.bottom - rect.top) * <span class="number">3</span>/ <span class="number">5</span>,</span><br><span class="line">			(rect.right - rect.left) * <span class="number">2</span> / <span class="number">7</span>, (rect.bottom - rect.top) * <span class="number">4</span>  / <span class="number">5</span>);</span><br><span class="line">		DeleteObject(SelectObject(hdc, hOldBrush));</span><br><span class="line"></span><br><span class="line">		hBrush = CreateHatchBrush(HS_HORIZONTAL, RGB(<span class="number">0</span>, <span class="number">255</span>, <span class="number">0</span>));</span><br><span class="line">		hOldBrush = SelectObject(hdc, hBrush);</span><br><span class="line">		Rectangle(hdc, (rect.right - rect.left) * <span class="number">3</span> / <span class="number">7</span>, (rect.bottom - rect.top) * <span class="number">3</span>/ <span class="number">5</span>,</span><br><span class="line">			(rect.right - rect.left) * <span class="number">4</span> / <span class="number">7</span>, (rect.bottom - rect.top) * <span class="number">4</span> / <span class="number">5</span>);</span><br><span class="line">		DeleteObject(SelectObject(hdc, hOldBrush));</span><br><span class="line"></span><br><span class="line">		hBrush = CreateHatchBrush(HS_VERTICAL, RGB(<span class="number">0</span>, <span class="number">255</span>, <span class="number">255</span>));</span><br><span class="line">		hOldBrush = SelectObject(hdc, hBrush);</span><br><span class="line">		Rectangle(hdc, (rect.right - rect.left) * <span class="number">5</span> / <span class="number">7</span>, (rect.bottom - rect.top)*<span class="number">3</span> / <span class="number">5</span>,</span><br><span class="line">			(rect.right - rect.left) * <span class="number">6</span> / <span class="number">7</span>, (rect.bottom - rect.top) * <span class="number">4</span> / <span class="number">5</span>);</span><br><span class="line">		DeleteObject(SelectObject(hdc, hOldBrush));</span><br><span class="line"></span><br><span class="line">		EndPaint(hdc, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="多边形"><a href="#多边形" class="headerlink" title="多边形"></a>多边形</h1><p>Polygon</p>
<p>PolyPolygon</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	POINT apt1[<span class="number">5</span>] = &#123; <span class="number">100</span>, <span class="number">100</span>, <span class="number">200</span>, <span class="number">100</span>, <span class="number">200</span>, <span class="number">200</span>, <span class="number">100</span>,<span class="number">200</span>, <span class="number">100</span>, <span class="number">100</span> &#125;;</span><br><span class="line">	POINT apt2[<span class="number">4</span>] = &#123; <span class="number">300</span>, <span class="number">100</span>, <span class="number">400</span>, <span class="number">100</span>, <span class="number">400</span>, <span class="number">200</span>, <span class="number">300</span>,<span class="number">200</span> &#125;;</span><br><span class="line">	HBRUSH hBrush, hOldBrush;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span>(message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		hBrush = CreateSolidBrush(RGB(<span class="number">255</span>, <span class="number">0</span>, <span class="number">0</span>));</span><br><span class="line">		hOldBrush = SelectObject(hdc, hBrush);</span><br><span class="line">		Polyline(hdc, apt1, <span class="number">5</span>);  <span class="comment">//画线</span></span><br><span class="line">		Polygon(hdc, apt2, <span class="number">4</span>);   <span class="comment">//多边形</span></span><br><span class="line"></span><br><span class="line">		DeleteObject(SelectObject(hdc, hOldBrush));</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">			PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">			<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>多边形填充模式：</p>
<p>SetPolyFillMode函数</p>
<ul>
<li>ALTERNATE：交替式，默认。遇到奇数边填充，偶数边不填充。</li>
<li>WINDING：螺旋式。填充一笔能够完成的区域。</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">SetPolyFillMode(WINDING);</span><br></pre></td></tr></table></figure>
<h1 id="映射模式"><a href="#映射模式" class="headerlink" title="映射模式"></a>映射模式</h1><p> 默认为MM_TEXT。</p>
<p><code>int iMapMode = GetMapMode(hdc)</code>获取当前映射模式。</p>
<p>通过<code>SetMapMode(hdc,MM_TEXT)</code>来设置。用完最后设置为默认模式。</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200504163819609.png" alt="image-20200504163819609"></p>
<p>逻辑单位和设备单位。</p>
<p>逻辑单位：独立与设备，编程用到的是逻辑单位。</p>
<p>设备单位：显示在显示器时，会将逻辑单位转换成设备单位，这个转换由映射模式决定的。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">int</span> iMapMode;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		TextOut(hdc, <span class="number">100</span>, <span class="number">100</span>, TEXT(<span class="string">"I love China"</span>), <span class="number">12</span>);</span><br><span class="line">		iMapMode = GetMapMode(hdc);</span><br><span class="line">		SetMapMode(hdc, MM_LOMETRIC);</span><br><span class="line">		TextOut(hdc, <span class="number">100</span>, <span class="number">-100</span>, TEXT(<span class="string">"I love Chinese"</span>), <span class="number">14</span>);</span><br><span class="line">		SetMapMode(hdc, iMapMode);</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="视口与窗口"><a href="#视口与窗口" class="headerlink" title="视口与窗口"></a>视口与窗口</h1><p>编程时，使用的是逻辑单位，所操作的范围，我们称之为窗口。</p>
<p>人眼所见时，是设备坐标，所见的范围，我们称之为对应视口。</p>
<p>设备坐标的单位是像素，映射模式决定逻辑坐标的单位。</p>
<p>绘制sin函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> NUM 1000</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> TWOPI (2*3.14159)</span></span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">int</span> iMapMode, i;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxClient, cyClient;</span><br><span class="line">	POINT apt[NUM];</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		cxClient = LOWORD(lParam);</span><br><span class="line">		cyClient = HIWORD(lParam);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">		MoveToEx(hdc, <span class="number">0</span>, cyClient / <span class="number">2</span>, <span class="literal">NULL</span>);</span><br><span class="line">		LineTo(hdc, cxClient, cyClient / <span class="number">2</span>);</span><br><span class="line"></span><br><span class="line">		MoveToEx(hdc, cxClient / <span class="number">2</span>, <span class="number">0</span>, <span class="literal">NULL</span>);</span><br><span class="line">		LineTo(hdc, cxClient / <span class="number">2</span>, cyClient);</span><br><span class="line"></span><br><span class="line">		iMapMode = GetMapMode(hdc);</span><br><span class="line">		SetMapMode(hdc, MM_LOMETRIC);	              <span class="comment">//重新设置映射模式       </span></span><br><span class="line">		SetViewportOrgEx(hdc, <span class="number">0</span>, cyClient / <span class="number">2</span>, <span class="literal">NULL</span>); <span class="comment">//重新设定原点</span></span><br><span class="line"></span><br><span class="line">		GetClientRect(hwnd, &amp;rect); </span><br><span class="line">		DPtoLP(hdc, (PPOINT)(&amp;rect), <span class="number">2</span>); <span class="comment">//设备坐标转换成逻辑坐标</span></span><br><span class="line">		<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; NUM; ++i) &#123;</span><br><span class="line">			apt[i].x = (rect.right - rect.left) * i / NUM;</span><br><span class="line">			apt[i].y = (<span class="keyword">int</span>)(rect.top * <span class="built_in">sin</span>(TWOPI * i / NUM));</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">		Polyline(hdc, apt, NUM);</span><br><span class="line">		SetMapMode(hdc, iMapMode);</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>自定义映射模式：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">int</span> iMapMode, i;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxClient, cyClient;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		cxClient = LOWORD(lParam);</span><br><span class="line">		cyClient = HIWORD(lParam);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line"></span><br><span class="line">		iMapMode = GetMapMode(hdc);</span><br><span class="line">		<span class="comment">//SetMapMode(hdc, MM_ANISOTROPIC); //不裁剪，而是将高度和宽度分为100份</span></span><br><span class="line">		SetMapMode(hdc, MM_ISOTROPIC);     <span class="comment">//裁剪</span></span><br><span class="line">		SetWindowExtEx(hdc, <span class="number">100</span>, <span class="number">100</span>, <span class="literal">NULL</span>);  <span class="comment">//设置逻辑单位</span></span><br><span class="line">		SetViewportExtEx(hdc, cxClient / <span class="number">2</span>, cyClient / <span class="number">2</span>, <span class="literal">NULL</span>); <span class="comment">//设置设备单位，结合逻辑单位计算比例</span></span><br><span class="line">		SetViewportOrgEx(hdc, cxClient / <span class="number">2</span>, cyClient / <span class="number">2</span>, <span class="literal">NULL</span>);</span><br><span class="line"></span><br><span class="line">		Rectangle(hdc, <span class="number">-100</span>, <span class="number">-100</span>, <span class="number">100</span>, <span class="number">100</span>);</span><br><span class="line">		Ellipse(hdc, <span class="number">-100</span>, <span class="number">-100</span>, <span class="number">100</span>, <span class="number">100</span>);</span><br><span class="line"></span><br><span class="line">		SetMapMode(hdc, iMapMode);</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p> MM_ISOTROPIC模式下，可以通过设置SetViewportExtEx()中设备单位的正负来改变坐标轴的方向。</p>
<p>默认情况下，y轴向下为正，x轴向右为正。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">SetMapMode(hdc, MM_ISOTROPIC);     <span class="comment">//裁剪</span></span><br><span class="line">SetWindowExtEx(hdc, <span class="number">100</span>, <span class="number">100</span>, <span class="literal">NULL</span>);  <span class="comment">//设置逻辑单位</span></span><br><span class="line">SetViewportExtEx(hdc, cxClient / <span class="number">2</span>, -cyClient / <span class="number">2</span>, <span class="literal">NULL</span>); <span class="comment">//设置设备单位，结合逻辑单位计算比例</span></span><br></pre></td></tr></table></figure>
<p>上述代码中，y轴向上为正，x轴向右为正。</p>
<h1 id="处理矩形"><a href="#处理矩形" class="headerlink" title="处理矩形"></a>处理矩形</h1><p>FillRect：填充矩形</p>
<p>FrameRect：画矩形的边框</p>
<p>InvertRect：矩形的填充色置为背景的取反</p>
<p>SetRect：初始化矩形</p>
<p>OffsetRect：偏移矩形</p>
<p>InflateRect：缩放矩形</p>
<p>SetRectEmpty：将矩形置为空</p>
<p>CopyRect：拷贝矩形</p>
<p>IntersectRect：求两个矩形的交集</p>
<p>UnionRect：求两个矩形的并集</p>
<p>IsRectEmpty：判断是一个矩形是否为空</p>
<p>PtInRect：检测一个点是否在矩形当中</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT ps;</span><br><span class="line">	RECT rect, rectC;</span><br><span class="line">	<span class="keyword">int</span> iMapMode, i;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxClient, cyClient;</span><br><span class="line">	HBRUSH hBrush;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		cxClient = LOWORD(lParam);</span><br><span class="line">		cyClient = HIWORD(lParam);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">		rect.top = cyClient / 2 - 50;</span></span><br><span class="line"><span class="comment">		rect.left = cxClient / 2 - 50;</span></span><br><span class="line"><span class="comment">		rect.bottom = cyClient / 2 + 50;</span></span><br><span class="line"><span class="comment">		rect.right = cxClient / 2 + 50;</span></span><br><span class="line"><span class="comment">		*/</span></span><br><span class="line">		SetRect(&amp;rect, cxClient / <span class="number">2</span> - <span class="number">50</span>, cyClient / <span class="number">2</span> - <span class="number">50</span>, cxClient / <span class="number">2</span> + <span class="number">50</span>, cyClient / <span class="number">2</span> + <span class="number">50</span>); <span class="comment">//对rect进行赋值</span></span><br><span class="line">		</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">		hBrush = CreateSolidBrush(RGB(<span class="number">255</span>, <span class="number">0</span>, <span class="number">0</span>));</span><br><span class="line">		OffsetRect(&amp;rect, <span class="number">-50</span>, <span class="number">-50</span>);   <span class="comment">//将矩形进行移动</span></span><br><span class="line">		FillRect(hdc, &amp;rect, hBrush);  <span class="comment">//填充矩形</span></span><br><span class="line">		FrameRect(hdc, &amp;rect, hBrush); <span class="comment">//画边框</span></span><br><span class="line">		InvertRect(hdc, &amp;rect);          <span class="comment">//颜色对背景色取反</span></span><br><span class="line"></span><br><span class="line">		SetRect(&amp;rectC, cxClient / <span class="number">2</span> + <span class="number">100</span>, cyClient / <span class="number">2</span> - <span class="number">50</span>, cxClient / <span class="number">2</span> + <span class="number">200</span>, cyClient / <span class="number">2</span> + <span class="number">50</span>); <span class="comment">//对rect进行赋值</span></span><br><span class="line">		InflateRect(&amp;rectC, <span class="number">100</span>, <span class="number">100</span>);    <span class="comment">//将矩形扩大缩小， 向上增加100，向下增加100，向左向右都增加100</span></span><br><span class="line">		FrameRect(hdc, &amp;rectC, hBrush); <span class="comment">//画边框</span></span><br><span class="line"></span><br><span class="line">		SetRectEmpty(&amp;rect); <span class="comment">//将矩形置空</span></span><br><span class="line">		CopyRect(&amp;rectC, &amp;rect); <span class="comment">//拷贝矩形</span></span><br><span class="line">		<span class="comment">//IntersectRect()</span></span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>PtInRect测试：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT ps;</span><br><span class="line">	<span class="keyword">static</span> RECT rect, rectC;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxClient, cyClient;</span><br><span class="line">	HBRUSH hBrush;</span><br><span class="line">	POINT point;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		cxClient = LOWORD(lParam);</span><br><span class="line">		cyClient = HIWORD(lParam);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_LBUTTONDOWN:</span><br><span class="line">		point.x = LOWORD(lParam);</span><br><span class="line">		point.y = HIWORD(lParam);</span><br><span class="line">		<span class="keyword">if</span> (PtInRect(&amp;rect, point)) &#123;</span><br><span class="line">			MessageBox(hwnd, TEXT(<span class="string">"被点中了！！"</span>), TEXT(<span class="string">"Demo"</span>), MB_OK);</span><br><span class="line">		&#125;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line"></span><br><span class="line">		SetRect(&amp;rect, cxClient / <span class="number">2</span> - <span class="number">50</span>, cyClient / <span class="number">2</span> - <span class="number">50</span>, cxClient / <span class="number">2</span> + <span class="number">50</span>, cyClient / <span class="number">2</span> + <span class="number">50</span>); <span class="comment">//对rect进行赋值</span></span><br><span class="line">		hBrush = CreateSolidBrush(RGB(<span class="number">255</span>, <span class="number">0</span>, <span class="number">0</span>));</span><br><span class="line">		FillRect(hdc, &amp;rect, hBrush);  <span class="comment">//填充矩形</span></span><br><span class="line"></span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="随机矩形"><a href="#随机矩形" class="headerlink" title="随机矩形"></a>随机矩形</h1><p>PeekMessage函数检查线程的消息队列并获取消息(如果存在的话)。</p>
<p>利用PeekMessage函数来实现利用程序执行的空余时间绘制矩形。具体代码如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;math.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> NUM 1000</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> TWOPI (2*3.14159)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND, UINT, WPARAM, LPARAM)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">DrawRectangle</span><span class="params">(HWND hwnd)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">int</span> cxClient, cyClient;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> WINAPI <span class="title">WinMain</span><span class="params">(HINSTANCE hInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	HINSTANCE hPrevInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	PSTR szCmdLine,</span></span></span><br><span class="line"><span class="function"><span class="params">	<span class="keyword">int</span> iCmdShow)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> TCHAR * szAppName = TEXT(<span class="string">"MyWindows"</span>);</span><br><span class="line">	HWND hwnd;            <span class="comment">//句柄，特殊的指针，找到窗口的标识</span></span><br><span class="line">	MSG msg;              <span class="comment">//消息结构，windwos核心就是消息机制</span></span><br><span class="line">	WNDCLASS wndclass;    <span class="comment">//窗口类结构</span></span><br><span class="line"></span><br><span class="line">	wndclass.style = CS_HREDRAW | CS_VREDRAW;                     <span class="comment">//窗口风格, CS_HERDRAW表示水平调整窗口会发生重绘</span></span><br><span class="line">	wndclass.lpfnWndProc = WndProc;                               <span class="comment">//窗口过程</span></span><br><span class="line">	wndclass.cbClsExtra = <span class="number">0</span>;                                      <span class="comment">//预留的额外空间</span></span><br><span class="line">	wndclass.cbWndExtra = <span class="number">0</span>;                                      <span class="comment">//</span></span><br><span class="line">	wndclass.hInstance = hInstance;                               <span class="comment">//实例句柄</span></span><br><span class="line">	wndclass.hIcon = LoadIcon(<span class="literal">NULL</span>, IDI_APPLICATION);             <span class="comment">//图标</span></span><br><span class="line">	wndclass.hCursor = LoadCursor(<span class="literal">NULL</span>, IDC_ARROW);               <span class="comment">//光标</span></span><br><span class="line">	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); <span class="comment">//背景</span></span><br><span class="line">	wndclass.lpszClassName = szAppName;                           <span class="comment">//窗口类的名字</span></span><br><span class="line">	wndclass.lpszMenuName = <span class="literal">NULL</span>;                                 <span class="comment">//菜单名字</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//注册窗口，传入窗口类结构指针</span></span><br><span class="line">	<span class="keyword">if</span> (!RegisterClass(&amp;wndclass)) &#123;<span class="comment">//返回NULL</span></span><br><span class="line">		MessageBox(<span class="literal">NULL</span>, TEXT(<span class="string">"这个改程序需要在Windows NT才能执行"</span>), szAppName, MB_ICONERROR);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//创建窗口</span></span><br><span class="line">	hwnd = CreateWindow(szAppName,  <span class="comment">//窗口类名称</span></span><br><span class="line">		TEXT(<span class="string">"中国"</span>),               <span class="comment">//窗口标题</span></span><br><span class="line">		WS_OVERLAPPEDWINDOW,        <span class="comment">//窗口风格</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x方向尺寸</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y方向尺寸</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//父窗口句柄</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//窗口菜单句柄</span></span><br><span class="line">		hInstance,                  <span class="comment">//程序实例句柄</span></span><br><span class="line">		<span class="literal">NULL</span>);                      <span class="comment">//创建参数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示窗口</span></span><br><span class="line">	ShowWindow(hwnd, iCmdShow);</span><br><span class="line">	<span class="comment">//更新窗口</span></span><br><span class="line">	UpdateWindow(hwnd);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//消息循环</span></span><br><span class="line">	<span class="keyword">while</span> (TRUE) &#123;<span class="comment">//WM_QUIT消息，返回0</span></span><br><span class="line">		<span class="keyword">if</span> (PeekMessage(&amp;msg, <span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">0</span>, PM_REMOVE)) &#123;</span><br><span class="line">			<span class="keyword">if</span> (msg.message == WM_QUIT)</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			TranslateMessage(&amp;msg);</span><br><span class="line">			DispatchMessage(&amp;msg);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			<span class="comment">//让程序空闲的时候执行的代码</span></span><br><span class="line">			DrawRectangle(hwnd);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> msg.wParam;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT ps;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		cxClient = LOWORD(lParam);</span><br><span class="line">		cyClient = HIWORD(lParam);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">DrawRectangle</span><span class="params">(HWND hwnd)</span> </span>&#123;</span><br><span class="line">	HBRUSH hBrush;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">if</span> (cxClient == <span class="number">0</span> || cyClient == <span class="number">0</span>)</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	SetRect(&amp;rect, rand() % cxClient, rand() % cyClient, rand() % cxClient, rand() % cyClient);</span><br><span class="line"></span><br><span class="line">	hBrush = CreateSolidBrush(RGB(rand() % <span class="number">256</span>, rand() % <span class="number">256</span>, rand() % <span class="number">256</span>));</span><br><span class="line">	</span><br><span class="line">	hdc = GetDC(hwnd);</span><br><span class="line">	FillRect(hdc, &amp;rect, hBrush);</span><br><span class="line">	DeleteObject(hBrush);</span><br><span class="line">	ReleaseDC(hwnd, hdc);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="区域和裁剪"><a href="#区域和裁剪" class="headerlink" title="区域和裁剪"></a>区域和裁剪</h1><p>区域是对显示器一块空间的描述，这个空间可以是矩形，多边形或椭圆的组合。</p>
<p>我们可以使用区域进行绘图或剪裁。</p>
<p>将区域选入设备环境，就可以使用这个区域来剪裁，也就是绘制动作限制在客户区的一个特定部分。</p>
<p>同画刷和画笔，区域也是一个GDI对象，应当通过调用DeleteObject函数来删除所建立的区域。</p>
<h2 id="创建区域："><a href="#创建区域：" class="headerlink" title="创建区域："></a>创建区域：</h2><p>创建区域的API函数：</p>
<h1 id="键盘"><a href="#键盘" class="headerlink" title="键盘"></a>键盘</h1><p>击键消息：点击鼠标一瞬间的消息，SYS表示属于系统关注的消息，交给window默认处理就可以，比如Alt+F4</p>
<ul>
<li>WM_KEYDOWN</li>
<li>WM_KEYUP</li>
<li>WM_SYSKEYDOWN</li>
<li>WM_SYSKEYUP</li>
</ul>
<p>字符消息：</p>
<ul>
<li>WM_CHAR</li>
<li>WM__DEADCHAR</li>
<li>WM_SYSCHAR</li>
<li>WM_SYSDEADCHAR</li>
</ul>
<p>SendMessage函数直接调用指定窗口过程。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line"></span><br><span class="line">	TCHAR szBuffer[<span class="number">10</span>];</span><br><span class="line">	<span class="keyword">size_t</span> iTarget;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxChar, cyChar, cxCaps;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxClient, cyClient, iVscrollPos;</span><br><span class="line">	TEXTMETRIC tm;</span><br><span class="line">	<span class="keyword">int</span> i, y;</span><br><span class="line"></span><br><span class="line">	SCROLLINFO si;</span><br><span class="line">	<span class="keyword">int</span> yPos, FirstLine, LastLine;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_CREATE:</span><br><span class="line">		hdc = GetDC(hwnd);</span><br><span class="line">		GetTextMetrics(hdc, &amp;tm);</span><br><span class="line">		cxChar = tm.tmAveCharWidth;     <span class="comment">//字符平均宽度</span></span><br><span class="line">		cxCaps = (tm.tmPitchAndFamily &amp; <span class="number">1</span> ? <span class="number">3</span> : <span class="number">2</span>) * cxChar / <span class="number">2</span>; <span class="comment">//如果低位为0,表示等宽字体，如果为1,表示变宽字体，大写字母是小写字母的1.5倍</span></span><br><span class="line">		cyChar = tm.tmHeight + tm.tmExternalLeading;</span><br><span class="line"></span><br><span class="line">		ReleaseDC(hwnd, hdc);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		hdc = GetDC(hwnd);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//设置滚动条，包括范围和页面大小</span></span><br><span class="line">		si.cbSize = <span class="keyword">sizeof</span>(si);</span><br><span class="line">		si.fMask = SIF_RANGE | SIF_PAGE;</span><br><span class="line">		si.nMax = NUMLINES<span class="number">-1</span>;</span><br><span class="line">		si.nMin = <span class="number">0</span>;</span><br><span class="line">		si.nPage = cyClient / cyChar;</span><br><span class="line">		SetScrollInfo(hwnd, SB_VERT, &amp;si, TRUE);</span><br><span class="line"></span><br><span class="line">		ReleaseDC(hwnd, hdc);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取滚动条位置</span></span><br><span class="line">		si.cbSize = <span class="keyword">sizeof</span>(si);</span><br><span class="line">		si.fMask = SIF_POS;</span><br><span class="line">		GetScrollInfo(hwnd, SB_VERT, &amp;si);</span><br><span class="line">		yPos = si.nPos;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//计算需要重绘的区域</span></span><br><span class="line">		FirstLine = max(<span class="number">0</span>, yPos + ps.rcPaint.top / cyChar);</span><br><span class="line">		LastLine = min(NUMLINES<span class="number">-1</span>, yPos + ps.rcPaint.bottom / cyChar);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//重绘失效区域</span></span><br><span class="line">		<span class="keyword">for</span> (i = FirstLine; i &lt;= LastLine; ++i) &#123;</span><br><span class="line">			y = cyChar * (i - yPos);</span><br><span class="line">			StringCchLength(sysMets[i].szContent, <span class="number">128</span>, &amp;iTarget);</span><br><span class="line">			TextOut(hdc, <span class="number">0</span>, y, sysMets[i].szContent, iTarget);</span><br><span class="line"></span><br><span class="line">			StringCchLength(sysMets[i].szDes, <span class="number">128</span>, &amp;iTarget);</span><br><span class="line">			TextOut(hdc, <span class="number">22</span> * cxCaps, y, sysMets[i].szDes, iTarget);</span><br><span class="line"></span><br><span class="line">			SetTextAlign(hdc, TA_TOP | TA_RIGHT);</span><br><span class="line">			StringCchPrintf(szBuffer, <span class="number">10</span>, TEXT(<span class="string">"%d"</span>), GetSystemMetrics(sysMets[i].iIndex));</span><br><span class="line">			StringCchLength(szBuffer, <span class="number">10</span>, &amp;iTarget);</span><br><span class="line">			TextOut(hdc, <span class="number">22</span> * cxCaps + <span class="number">40</span> * cxChar, y, szBuffer, iTarget);</span><br><span class="line"></span><br><span class="line">			SetTextAlign(hdc, TA_TOP | TA_LEFT);</span><br><span class="line"></span><br><span class="line">		&#125;</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_VSCROLL:</span><br><span class="line">		<span class="comment">//获取滚动条的位置</span></span><br><span class="line">		si.cbSize = <span class="keyword">sizeof</span>(si);</span><br><span class="line">		si.fMask = SIF_ALL;</span><br><span class="line">		GetScrollInfo(hwnd, SB_VERT, &amp;si);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//记录当前滚动条位置，以为了后续判断是否滚动条位置发生变化</span></span><br><span class="line">		yPos = si.nPos;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//响应各种操作</span></span><br><span class="line">		<span class="keyword">switch</span> (LOWORD(wParam))</span><br><span class="line">		&#123;</span><br><span class="line">		<span class="keyword">case</span> SB_TOP:</span><br><span class="line">			si.nPos = si.nMin;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_BOTTOM:</span><br><span class="line">			si.nPos = si.nMax;</span><br><span class="line">		<span class="keyword">case</span> SB_LINEUP:</span><br><span class="line">			si.nPos -= <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_LINEDOWN:</span><br><span class="line">			si.nPos += <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_PAGEUP:</span><br><span class="line">			si.nPos -= si.nPage;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_PAGEDOWN:</span><br><span class="line">			si.nPos += si.nPage;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_THUMBTRACK:</span><br><span class="line">			si.nPos = si.nTrackPos;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//设置滚动条位置</span></span><br><span class="line">		si.fMask = SIF_POS;</span><br><span class="line">		SetScrollInfo(hwnd, SB_VERT, &amp;si, TRUE);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//重新获取滚动条位置</span></span><br><span class="line">		GetScrollInfo(hwnd, SB_VERT, &amp;si);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//判断滚动条位置是否发生变化</span></span><br><span class="line">		<span class="keyword">if</span> (yPos != si.nPos) &#123;</span><br><span class="line">			<span class="comment">//滚动客户区的内容</span></span><br><span class="line">			ScrollWindow(hwnd, <span class="number">0</span>, cyChar * (yPos - si.nPos), <span class="literal">NULL</span>, <span class="literal">NULL</span>);</span><br><span class="line">			<span class="comment">//发生WM_PAINT消息，重新绘制失效区域</span></span><br><span class="line">			UpdateWindow(hwnd);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_KEYDOWN:</span><br><span class="line">		<span class="keyword">switch</span> (LOWORD(wParam)) &#123;</span><br><span class="line">		<span class="keyword">case</span> VK_UP:</span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, <span class="number">0</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_DOWN:</span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, <span class="number">0</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_PRIOR:</span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, <span class="number">0</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_NEXT:</span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, <span class="number">0</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_HOME:</span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_TOP, <span class="number">0</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_END:</span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_BOTTOM, <span class="number">0</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>字符消息：</p>
<p>WM_DEADCHAR：有些国家的字母是有符号，比如A上有两个点等等。</p>
<p>消息的参数：</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200505163631841.png" alt="image-20200505163631841"></p>
<p>如果RegisterClass使用的是RegisterClassW使用的就是Unicode，如果使用的是RegisterClassA就使用的ANSI，或者是使用IsWindowUnicode来判断。</p>
<p>用户按下A键并释放时，窗口过程会接收到以下三个消息：</p>
<p>  <img src="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200505163410839.png" alt="image-20200505163410839"></p>
<p>如果用户先按下Shift，在按下A，释放A按键，在释放Shift按键，窗口过程收到5个消息：</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200505164145565.png" alt="image-20200505164145565"></p>
<p>如果持续按下A键，那么会连续产生击键消息和相应的字符消息。</p>
<h1 id="插入符号"><a href="#插入符号" class="headerlink" title="插入符号"></a>插入符号</h1><p>5个基本的插入符号函数：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">CreateCaret();   <span class="comment">//创建和窗口相关的插入符号</span></span><br><span class="line">SetCaretPos();   <span class="comment">//设置窗口内的插入符号的位置</span></span><br><span class="line">ShowCaret();     <span class="comment">//显示插入符号</span></span><br><span class="line">HideCaret();     <span class="comment">//隐藏插入符号</span></span><br><span class="line">DestroyCaret();  <span class="comment">//销毁插入符号</span></span><br></pre></td></tr></table></figure>
<p>通过响应WM_SETFOCUS消息和WM_KILLFOCUS消息来判断窗口是否具备输入焦点。</p>
<p>TextEdit的实现：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> BUFFER(x, y) *(pBuffer + y * cxBuffer + x)</span></span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND, UINT, WPARAM, LPARAM)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> WINAPI <span class="title">WinMain</span><span class="params">(HINSTANCE hInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	HINSTANCE hPrevInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	PSTR szCmdLine,</span></span></span><br><span class="line"><span class="function"><span class="params">	<span class="keyword">int</span> iCmdShow)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> TCHAR * szAppName = TEXT(<span class="string">"MyWindows"</span>);</span><br><span class="line">	HWND hwnd;            <span class="comment">//句柄，特殊的指针，找到窗口的标识</span></span><br><span class="line">	MSG msg;              <span class="comment">//消息结构，windwos核心就是消息机制</span></span><br><span class="line">	WNDCLASS wndclass;    <span class="comment">//窗口类结构</span></span><br><span class="line"></span><br><span class="line">	wndclass.style = CS_HREDRAW | CS_VREDRAW;                     <span class="comment">//窗口风格, CS_HERDRAW表示水平调整窗口会发生重绘</span></span><br><span class="line">	wndclass.lpfnWndProc = WndProc;                               <span class="comment">//窗口过程</span></span><br><span class="line">	wndclass.cbClsExtra = <span class="number">0</span>;                                      <span class="comment">//预留的额外空间</span></span><br><span class="line">	wndclass.cbWndExtra = <span class="number">0</span>;                                      <span class="comment">//</span></span><br><span class="line">	wndclass.hInstance = hInstance;                               <span class="comment">//实例句柄</span></span><br><span class="line">	wndclass.hIcon = LoadIcon(<span class="literal">NULL</span>, IDI_APPLICATION);             <span class="comment">//图标</span></span><br><span class="line">	wndclass.hCursor = LoadCursor(<span class="literal">NULL</span>, IDC_ARROW);               <span class="comment">//光标</span></span><br><span class="line">	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); <span class="comment">//背景</span></span><br><span class="line">	wndclass.lpszClassName = szAppName;                           <span class="comment">//窗口类的名字</span></span><br><span class="line">	wndclass.lpszMenuName = <span class="literal">NULL</span>;                                 <span class="comment">//菜单名字</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//注册窗口，传入窗口类结构指针</span></span><br><span class="line">	<span class="keyword">if</span> (!RegisterClass(&amp;wndclass)) &#123;<span class="comment">//返回NULL</span></span><br><span class="line">		MessageBox(<span class="literal">NULL</span>, TEXT(<span class="string">"这个改程序需要在Windows NT才能执行"</span>), szAppName, MB_ICONERROR);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//创建窗口</span></span><br><span class="line">	hwnd = CreateWindow(szAppName,  <span class="comment">//窗口类名称</span></span><br><span class="line">		TEXT(<span class="string">"中国"</span>),               <span class="comment">//窗口标题</span></span><br><span class="line">		WS_OVERLAPPEDWINDOW,        <span class="comment">//窗口风格</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x方向尺寸</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y方向尺寸</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//父窗口句柄</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//窗口菜单句柄</span></span><br><span class="line">		hInstance,                  <span class="comment">//程序实例句柄</span></span><br><span class="line">		<span class="literal">NULL</span>);                      <span class="comment">//创建参数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示窗口</span></span><br><span class="line">	ShowWindow(hwnd, iCmdShow);</span><br><span class="line">	<span class="comment">//更新窗口</span></span><br><span class="line">	UpdateWindow(hwnd);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//消息循环</span></span><br><span class="line">	<span class="keyword">while</span> (GetMessage(&amp;msg, <span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">0</span>)) &#123;<span class="comment">//WM_QUIT消息，返回0</span></span><br><span class="line">		TranslateMessage(&amp;msg);</span><br><span class="line">		DispatchMessage(&amp;msg);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> msg.wParam;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	<span class="keyword">static</span> DWORD dwCharSet = DEFAULT_CHARSET;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer, cxCaret, cyCaret;</span><br><span class="line">	TEXTMETRIC tm;</span><br><span class="line">	<span class="keyword">static</span> TCHAR * pBuffer = <span class="literal">NULL</span>;</span><br><span class="line">	<span class="keyword">int</span> x, y, i;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_CREATE:</span><br><span class="line">		hdc = GetDC(hwnd);</span><br><span class="line"></span><br><span class="line">		SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT)); <span class="comment">//设置为等宽字符，方便计算插入符的位置</span></span><br><span class="line">		GetTextMetrics(hdc, &amp;tm);</span><br><span class="line">		cxChar = tm.tmAveCharWidth;</span><br><span class="line">		cyChar = tm.tmHeight + tm.tmExternalLeading;</span><br><span class="line"></span><br><span class="line">		ReleaseDC(hwnd, hdc);</span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		<span class="keyword">if</span> (message == WM_SIZE) &#123;</span><br><span class="line">			cxClient = LOWORD(lParam);</span><br><span class="line">			cyClient = HIWORD(lParam);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		cxBuffer = max(cxClient / cxChar, <span class="number">1</span>);</span><br><span class="line">		cyBuffer = max(cyClient / cyChar, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (pBuffer != <span class="literal">NULL</span>) &#123;</span><br><span class="line">			<span class="built_in">free</span>(pBuffer);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		pBuffer = (TCHAR *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(TCHAR) * cxBuffer * cyBuffer);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">for</span> (y = <span class="number">0</span>; y &lt; cyBuffer; ++y) &#123;</span><br><span class="line">			<span class="keyword">for</span> (x = <span class="number">0</span>; x &lt; cxBuffer; ++x) &#123;</span><br><span class="line">				BUFFER(x, y) = <span class="string">' '</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		cxCaret = <span class="number">0</span>;</span><br><span class="line">		cyCaret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (hwnd == GetFocus()) &#123;</span><br><span class="line">			SetCaretPos(cxCaret*cxChar, cyCaret*cyChar);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		InvalidateRect(hwnd, <span class="literal">NULL</span>, TRUE); <span class="comment">//重绘整个窗口</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_SETFOCUS:</span><br><span class="line">		CreateCaret(hwnd, <span class="literal">NULL</span>, cxChar, cyChar);</span><br><span class="line">		SetCaretPos(cxCaret * cxChar, cyCaret * cyChar);</span><br><span class="line">		ShowCaret(hwnd);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_KILLFOCUS:</span><br><span class="line">		HideCaret(hwnd);</span><br><span class="line">		DestroyCaret();</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_KEYDOWN:</span><br><span class="line">		<span class="keyword">switch</span> (wParam) &#123;</span><br><span class="line">		<span class="keyword">case</span> VK_HOME:</span><br><span class="line">			cxCaret = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_END:</span><br><span class="line">			cxCaret = cxBuffer - <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_PRIOR:</span><br><span class="line">			cyCaret = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_NEXT:</span><br><span class="line">			cyCaret = cyBuffer - <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_LEFT:</span><br><span class="line">			cxCaret = max(<span class="number">0</span>, cxCaret - <span class="number">1</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_RIGHT:</span><br><span class="line">			cxCaret = min(cxCaret + <span class="number">1</span>, cxBuffer - <span class="number">1</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_UP:</span><br><span class="line">			cyCaret = max(<span class="number">0</span>, cyCaret - <span class="number">1</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_DOWN:</span><br><span class="line">			cyCaret = min(cyCaret + <span class="number">1</span>, cyBuffer - <span class="number">1</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_DELETE:</span><br><span class="line">			<span class="keyword">for</span> (x = cxCaret; x &lt; cxBuffer<span class="number">-1</span>; ++x) &#123;</span><br><span class="line">				BUFFER(x, cyCaret) = BUFFER(x + <span class="number">1</span>, cyCaret);</span><br><span class="line">			&#125;</span><br><span class="line">			BUFFER(cxBuffer<span class="number">-1</span>, cyCaret) = <span class="string">' '</span>;</span><br><span class="line"></span><br><span class="line">			HideCaret(hwnd);</span><br><span class="line">			hdc = GetDC(hwnd);</span><br><span class="line">			SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));</span><br><span class="line">			TextOut(hdc, cxCaret * cxChar, cyCaret * cyChar, &amp;BUFFER(cxCaret, cyCaret), cxBuffer - cxCaret);</span><br><span class="line">			ReleaseDC(hwnd, hdc);</span><br><span class="line">			ShowCaret(hwnd);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		SetCaretPos(cxCaret * cxChar, cyCaret * cyChar);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_CHAR:</span><br><span class="line">		<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; (<span class="keyword">int</span>)LOWORD(lParam); ++i) &#123; <span class="comment">//低16位表示重复的次数</span></span><br><span class="line">			<span class="keyword">switch</span> (wParam) &#123;</span><br><span class="line">			<span class="keyword">case</span> <span class="string">'\b'</span>: <span class="comment">//退格</span></span><br><span class="line">				<span class="keyword">if</span> (cxCaret &gt; <span class="number">0</span>) &#123;</span><br><span class="line">					--cxCaret;</span><br><span class="line">					SendMessage(hwnd, WM_KEYDOWN, VK_DELETE, <span class="number">1</span>);</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">case</span> <span class="string">'\t'</span>:</span><br><span class="line">				<span class="keyword">do</span> &#123;</span><br><span class="line">					SendMessage(hwnd, WM_CHAR, <span class="string">' '</span>, <span class="number">1</span>);</span><br><span class="line">				&#125; <span class="keyword">while</span> (cxCaret % <span class="number">8</span> != <span class="number">0</span>);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">case</span> <span class="string">'\n'</span>:</span><br><span class="line">				<span class="keyword">if</span> (++cyBuffer == cyBuffer) &#123;</span><br><span class="line">					cyCaret = <span class="number">0</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			<span class="keyword">case</span> <span class="string">'\r'</span>:</span><br><span class="line">				cxCaret = <span class="number">0</span>;</span><br><span class="line">				<span class="keyword">if</span> (++cyCaret == cyBuffer) &#123;</span><br><span class="line">					cyBuffer = <span class="number">0</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			case '\x1B': //十六进制18，对应ASCII字符是ESC</span><br><span class="line">				<span class="keyword">for</span> (y = <span class="number">0</span>; y &lt; cyBuffer; ++y) &#123;</span><br><span class="line">					<span class="keyword">for</span> (x = <span class="number">0</span>; x &lt; cxBuffer; ++x)</span><br><span class="line">						BUFFER(x, y) = TEXT(<span class="string">' '</span>);</span><br><span class="line">				&#125;</span><br><span class="line">				cxCaret = <span class="number">0</span>, cyCaret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">				InvalidateRect(hwnd, <span class="literal">NULL</span>, FALSE);</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">			<span class="keyword">default</span>:</span><br><span class="line">				BUFFER(cxCaret, cyCaret) = (TCHAR)wParam;</span><br><span class="line"></span><br><span class="line">				HideCaret(hwnd);</span><br><span class="line">				hdc = GetDC(hwnd);</span><br><span class="line">				SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));</span><br><span class="line">				TextOut(hdc, cxCaret * cxChar, cyCaret * cyChar, &amp;BUFFER(cxCaret, cyCaret), <span class="number">1</span>);</span><br><span class="line">				ShowCaret(hwnd);</span><br><span class="line">				ReleaseDC(hwnd, hdc);</span><br><span class="line"></span><br><span class="line">				<span class="keyword">if</span> (++cxCaret == cxBuffer) &#123;</span><br><span class="line">					cxCaret = <span class="number">0</span>;</span><br><span class="line">					<span class="keyword">if</span> (++cyCaret == cyBuffer) &#123;</span><br><span class="line">						cyCaret = <span class="number">0</span>;</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		SetCaretPos(cxCaret*cxChar, cyCaret * cyChar);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		</span><br><span class="line">		SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));</span><br><span class="line">		<span class="keyword">for</span> (y = <span class="number">0</span>; y &lt; cyBuffer; ++y) &#123;</span><br><span class="line">			TextOut(hdc, <span class="number">0</span>, y*cyChar, &amp;BUFFER(<span class="number">0</span>, y), cxBuffer);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="客户区鼠标消息"><a href="#客户区鼠标消息" class="headerlink" title="客户区鼠标消息"></a>客户区鼠标消息</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fMouse = GetSystemMetrics(SM_MOUSEPRESENT);</span><br></pre></td></tr></table></figure>
<p>判断是否链接鼠标，如果鼠标已被安装，fMouse的值是非0，否则是0。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cButtons = GetSystemMetrics(SM_CMOUSEBUTTONS);</span><br></pre></td></tr></table></figure>
<p>判断鼠标的按钮个数。</p>
<p>基本术语：</p>
<ul>
<li>热点（hot spot）：鼠标指针的精确位置</li>
<li>单机</li>
<li>双击</li>
<li>拖动</li>
<li>LBUUTON，MBUTTON，RBUTTON</li>
</ul>
<p>收到鼠标消息，不一定需要具备焦点，这跟键盘消息不同。</p>
<p>只要有鼠标</p>
<p>窗口的客户区上移动，窗口过程会收到WM_MOUSEMOVE消息。</p>
<p>在客户区内按下或释放鼠标按钮时，窗口过程也会收到相应的消息：</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506114157779.png" alt="image-20200506114157779"></p>
<p>lParam参数：</p>
<p>对于收到的这些消息，其lParam参数都包含了触发该消息时鼠标的位置信息：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">LOWORD(lParam);  <span class="comment">//x坐标</span></span><br><span class="line">HIWORD(lParam);  <span class="comment">//y坐标</span></span><br></pre></td></tr></table></figure>
<p>现在不推荐使用上面的两个宏来获取鼠标的坐标，因为在多显示器的情况下会得到错误的坐标，应该使用：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windowsx.h&gt; //需要包含该头文件</span></span></span><br><span class="line">GET_X_LPARAM(lParam)</span><br><span class="line">GET_Y_LPARAM(lParam)</span><br></pre></td></tr></table></figure>
<p>wParam参数：</p>
<p>它用来表示鼠标按钮，Shitf键，Ctrl键的状态：</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506114557378.png" alt="image-20200506114557378"></p>
<p>比如当收到鼠标消息时，<code>wParam &amp; WM_LBUTTON</code>的值为TRUE，则表示此时鼠标左键是被按下的状态。</p>
<p>对于鼠标双击。</p>
<p>如果窗口类型没有包含说<code>CS_DBLCLKS</code>，则双击过程中产生的消息：</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506143425257.png" alt="image-20200506143425257"></p>
<p>如果窗口类型包含<code>CS_DBLCLKS</code>，则产生的消息为：</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506143458506.png" alt="image-20200506143458506"></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;                    </span><br><span class="line"><span class="comment">//窗口风格, CS_HERDRAW表示水平调整窗口会发生重绘</span></span><br></pre></td></tr></table></figure>
<p>在捕获双击的时候，需要注意不能存在捕获单机的消息，不然会无法捕获到双击的消息，这个时候需要注意逻辑问题。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> iCount;</span><br><span class="line">	<span class="keyword">int</span> i, j;</span><br><span class="line">	<span class="keyword">static</span> POINT apt[<span class="number">1000</span>];</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_LBUTTONDBLCLK:                   <span class="comment">//捕获双击消息，如果有捕获单击消息，需要注意逻辑，不然没发捕获双击消息</span></span><br><span class="line">		MessageBox(hwnd, TEXT(<span class="string">"666666"</span>), TEXT(<span class="string">"提示"</span>), MB_OK);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_LBUTTONDOWN:                 <span class="comment">//鼠标点下清空窗口</span></span><br><span class="line">		<span class="keyword">if</span> (wParam &amp; MK_SHIFT) &#123;    <span class="comment">//判断是否同时按下了Shift或Control</span></span><br><span class="line">			<span class="keyword">if</span> (wParam &amp; MK_CONTROL) &#123;</span><br><span class="line">				MessageBox(hwnd, TEXT(<span class="string">"同时按下了Ctrl和Shift键"</span>), TEXT(<span class="string">"提醒"</span>), MB_OK);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span> &#123;</span><br><span class="line">				MessageBox(hwnd, TEXT(<span class="string">"同时按下了Shift键"</span>), TEXT(<span class="string">"提醒"</span>), MB_OK);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			<span class="keyword">if</span> (wParam &amp; MK_CONTROL) &#123;</span><br><span class="line">				MessageBox(hwnd, TEXT(<span class="string">"同时按下了Control键"</span>), TEXT(<span class="string">"提醒"</span>), MB_OK);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span> &#123;</span><br><span class="line">				MessageBox(hwnd, TEXT(<span class="string">"没有按下了Shift和Control键"</span>), TEXT(<span class="string">"提醒"</span>), MB_OK);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		iCount = <span class="number">0</span>;</span><br><span class="line">		InvalidateRect(hwnd, <span class="literal">NULL</span>, TRUE);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_MOUSEMOVE:                     <span class="comment">//鼠标移动绘制点，并记录点</span></span><br><span class="line">		<span class="keyword">if</span> (wParam &amp;  MK_LBUTTON &amp;&amp; iCount &lt; <span class="number">1000</span>) &#123;</span><br><span class="line">			apt[iCount].x = LOWORD(lParam);</span><br><span class="line">			apt[iCount].y = HIWORD(lParam);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">			hdc = GetDC(hwnd);</span><br><span class="line">			SetPixel(hdc, apt[iCount].x, apt[iCount].y, RGB(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>));</span><br><span class="line">			ReleaseDC(hwnd, hdc);</span><br><span class="line"></span><br><span class="line">			++iCount;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_LBUTTONUP:                  <span class="comment">//鼠标左键松开，重绘窗口，不重绘背景，并触发WM_PAINT消息</span></span><br><span class="line">		InvalidateRect(hwnd, <span class="literal">NULL</span>, FALSE);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line"></span><br><span class="line">		SetCursor(LoadCursor(<span class="literal">NULL</span>, IDC_WAIT)); <span class="comment">//设置鼠标为加载状态</span></span><br><span class="line">		ShowCursor(TRUE);                      <span class="comment">//显示鼠标</span></span><br><span class="line"></span><br><span class="line">		<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; iCount - <span class="number">1</span>; ++i) &#123;</span><br><span class="line">			<span class="keyword">for</span> (j = i + <span class="number">1</span>; j &lt; iCount; ++j) &#123;</span><br><span class="line">				MoveToEx(hdc, apt[i].x, apt[i].y, <span class="literal">NULL</span>);</span><br><span class="line">				LineTo(hdc, apt[j].x, apt[j].y);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		ShowCursor(FALSE);                       </span><br><span class="line">		SetCursor(LoadCursor(<span class="literal">NULL</span>, IDC_ARROW));</span><br><span class="line"></span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="非客户区鼠标消息"><a href="#非客户区鼠标消息" class="headerlink" title="非客户区鼠标消息"></a>非客户区鼠标消息</h1><p>只要鼠标在窗口的非客户区上移动，窗口过程会收到<code>WM_NCMOUSEMOVE</code>消息。</p>
<p>在非客户区内按下或释放鼠标按钮时，窗口过程也会收到响应的消息：</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506144228796.png" alt="image-20200506144228796"></p>
<p>lParam参数：</p>
<p>对于收到的这些消息，其lParam参数都包含了触发该消息时鼠标的位置信息：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">LOWORD(lParam);  <span class="comment">//x坐标</span></span><br><span class="line">HIWORD(lParam);  <span class="comment">//y坐标</span></span><br></pre></td></tr></table></figure>
<p>现在不推荐使用上面的两个宏来获取鼠标的坐标，因为在多显示器的情况下会得到错误的坐标，应该使用：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windowsx.h&gt; //需要包含该头文件</span></span></span><br><span class="line">GET_X_LPARAM(lParam)</span><br><span class="line">GET_Y_LPARAM(lParam)</span><br></pre></td></tr></table></figure>
<p>利用下边的函数可以互相转换屏幕坐标和客户区坐标：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ScreenToClient(hwnd, &amp;pt); <span class="comment">//POINT pt;</span></span><br><span class="line">ClientToScreen(hwnd, &amp;pt);</span><br></pre></td></tr></table></figure>
<p>wParam参数：</p>
<p>与客户区鼠标消息不同，非客户区鼠标消息的wParm消息的值被设定为一个以“HT”为首的标识符，用于表示非客户区鼠标移动或点击的位置。</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506144831002.png" alt="image-20200506144831002"></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windowsx.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;strsafe.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	TCHAR pszDest[<span class="number">200</span>];</span><br><span class="line">	<span class="keyword">size_t</span> cChDest = <span class="number">200</span>;</span><br><span class="line">	LPCSTR pszFormat = TEXT(<span class="string">"关键按钮被按下，当前鼠标的坐标为(屏幕坐标)：x = %d, y = %d,转换为客户区坐标后是：x = %d, y = %d"</span>);</span><br><span class="line">	POINT ptScreen, ptClient;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_NCLBUTTONDOWN:</span><br><span class="line">		<span class="keyword">if</span> (wParam == HTCLOSE)&#123;</span><br><span class="line">			ptScreen.x = GET_X_LPARAM(lParam);</span><br><span class="line">			ptScreen.y = GET_Y_LPARAM(wParam);</span><br><span class="line">			ptClient.x = ptScreen.x;</span><br><span class="line">			ptClient.y = ptScreen.y;</span><br><span class="line">			ScreenToClient(hwnd, &amp;ptClient);</span><br><span class="line">			StringCchPrintf(pszDest, cChDest, pszFormat, ptScreen.x, ptScreen.y, ptClient.x, ptClient.y);</span><br><span class="line">			MessageBox(hwnd, pszDest, TEXT(<span class="string">"提醒"</span>), MB_OK);</span><br><span class="line">			SendMessage(hwnd, WM_CLOSE, <span class="number">0</span>, <span class="number">0</span>);   <span class="comment">//保证窗口能过关闭</span></span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam); <span class="comment">//处理其他客户区鼠标消息</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		GetClientRect(hwnd, &amp;rect);</span><br><span class="line">		DrawText(hdc, TEXT(<span class="string">"大家好，这是我的点第一个窗口程序！"</span>), <span class="number">-1</span>, &amp;rect,</span><br><span class="line">			DT_SINGLELINE | DT_CENTER | DT_VCENTER);</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>击中测试消息：</p>
<p>WM_NCHITTEST</p>
<p>鼠标点击实例：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windowsx.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> DIVISIONS 5</span></span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	<span class="keyword">static</span> BOOL state[DIVISIONS][DIVISIONS];</span><br><span class="line">	PAINTSTRUCT ps;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> x, y, cxBlock, cyBlock;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		cxBlock = GET_X_LPARAM(lParam) / DIVISIONS;</span><br><span class="line">		cyBlock = GET_Y_LPARAM(lParam) / DIVISIONS;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_LBUTTONDOWN:</span><br><span class="line">		x = GET_X_LPARAM(lParam) / cxBlock;</span><br><span class="line">		y = GET_Y_LPARAM(lParam) / cyBlock;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (x &lt; DIVISIONS &amp;&amp; y &lt; DIVISIONS) &#123;</span><br><span class="line">			state[x][y] ^= <span class="number">1</span>;</span><br><span class="line">			rect.left = x * cxBlock;</span><br><span class="line">			rect.top = y * cyBlock;</span><br><span class="line">			rect.bottom = (y + <span class="number">1</span>)*cyBlock;</span><br><span class="line">			rect.right = (x + <span class="number">1</span>)*cxBlock;</span><br><span class="line">			InvalidateRect(hwnd, <span class="literal">NULL</span>, FALSE);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			MessageBeep(<span class="number">0</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">for</span> (x = <span class="number">0</span>; x &lt; DIVISIONS; ++x) &#123;</span><br><span class="line">			<span class="keyword">for</span> (y = <span class="number">0</span>; y &lt; DIVISIONS; ++y) &#123;</span><br><span class="line">				Rectangle(hdc, x * cxBlock, y *cyBlock, (x + <span class="number">1</span>)*cxBlock, (y + <span class="number">1</span>)*cyBlock);</span><br><span class="line">				<span class="keyword">if</span> (state[x][y]) &#123;</span><br><span class="line">					MoveToEx(hdc, x * cxBlock, y * cyBlock, <span class="literal">NULL</span>);</span><br><span class="line">					LineTo(hdc, (x + <span class="number">1</span>)*cxBlock, (y + <span class="number">1</span>)*cyBlock);</span><br><span class="line">					MoveToEx(hdc, x * cxBlock, (y + <span class="number">1</span>) * cyBlock, <span class="literal">NULL</span>);</span><br><span class="line">					LineTo(hdc, (x + <span class="number">1</span>)*cxBlock, y*cyBlock);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="键盘模拟鼠标操作"><a href="#键盘模拟鼠标操作" class="headerlink" title="键盘模拟鼠标操作"></a>键盘模拟鼠标操作</h1><p>使用跟鼠标点击相同的实例，通过键盘在方格内移动。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	<span class="keyword">static</span> BOOL state[DIVISIONS][DIVISIONS];</span><br><span class="line">	PAINTSTRUCT ps;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> x, y, cxBlock, cyBlock;</span><br><span class="line">	RECT rect;</span><br><span class="line">	POINT point;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		cxBlock = GET_X_LPARAM(lParam) / DIVISIONS;</span><br><span class="line">		cyBlock = GET_Y_LPARAM(lParam) / DIVISIONS;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_KEYDOWN:</span><br><span class="line">		GetCursorPos(&amp;point);</span><br><span class="line">		ScreenToClient(hwnd, &amp;point);</span><br><span class="line"></span><br><span class="line">		x = max(<span class="number">0</span>, min(DIVISIONS - <span class="number">1</span>, point.x / cxBlock));</span><br><span class="line">		y = max(<span class="number">0</span>, min(DIVISIONS - <span class="number">1</span>, point.y / cyBlock));</span><br><span class="line"></span><br><span class="line">		<span class="keyword">switch</span> (wParam) &#123;</span><br><span class="line">		<span class="keyword">case</span> VK_UP:</span><br><span class="line">			--y;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_DOWN:</span><br><span class="line">			++y;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_LEFT:</span><br><span class="line">			--x;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_RIGHT:</span><br><span class="line">			++x;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_HOME:</span><br><span class="line">			x = y = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_END:</span><br><span class="line">			x = y = DIVISIONS - <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_RETURN:</span><br><span class="line">		<span class="keyword">case</span> VK_SPACE:</span><br><span class="line">			SendMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(x*cxBlock, y*cyBlock));</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		x = (x + DIVISIONS) % DIVISIONS;</span><br><span class="line">		y = (y + DIVISIONS) % DIVISIONS;</span><br><span class="line"></span><br><span class="line">		point.x = x * cxBlock + cxBlock / <span class="number">2</span>;</span><br><span class="line">		point.y = y * cyBlock + cyBlock / <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">		ClientToScreen(hwnd, &amp;point);</span><br><span class="line">		SetCursorPos(point.x, point.y);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_SETFOCUS:</span><br><span class="line">		ShowCursor(TRUE);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_KILLFOCUS:</span><br><span class="line">		ShowCursor(FALSE);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_LBUTTONDOWN:</span><br><span class="line">		x = GET_X_LPARAM(lParam) / cxBlock;</span><br><span class="line">		y = GET_Y_LPARAM(lParam) / cyBlock;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (x &lt; DIVISIONS &amp;&amp; y &lt; DIVISIONS) &#123;</span><br><span class="line">			state[x][y] ^= <span class="number">1</span>;</span><br><span class="line">			rect.left = x * cxBlock;</span><br><span class="line">			rect.top = y * cyBlock;</span><br><span class="line">			rect.bottom = (y + <span class="number">1</span>)*cyBlock;</span><br><span class="line">			rect.right = (x + <span class="number">1</span>)*cxBlock;</span><br><span class="line">			InvalidateRect(hwnd, <span class="literal">NULL</span>, FALSE);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			MessageBeep(<span class="number">0</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">for</span> (x = <span class="number">0</span>; x &lt; DIVISIONS; ++x) &#123;</span><br><span class="line">			<span class="keyword">for</span> (y = <span class="number">0</span>; y &lt; DIVISIONS; ++y) &#123;</span><br><span class="line">				Rectangle(hdc, x * cxBlock, y *cyBlock, (x + <span class="number">1</span>)*cxBlock, (y + <span class="number">1</span>)*cyBlock);</span><br><span class="line">				<span class="keyword">if</span> (state[x][y]) &#123;</span><br><span class="line">					MoveToEx(hdc, x * cxBlock, y * cyBlock, <span class="literal">NULL</span>);</span><br><span class="line">					LineTo(hdc, (x + <span class="number">1</span>)*cxBlock, (y + <span class="number">1</span>)*cyBlock);</span><br><span class="line">					MoveToEx(hdc, x * cxBlock, (y + <span class="number">1</span>) * cyBlock, <span class="literal">NULL</span>);</span><br><span class="line">					LineTo(hdc, (x + <span class="number">1</span>)*cxBlock, y*cyBlock);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="子窗口"><a href="#子窗口" class="headerlink" title="子窗口"></a>子窗口</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windowsx.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> DIVISIONS 5</span></span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND, UINT, WPARAM, LPARAM)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">ChildWndProc</span><span class="params">(HWND, UINT, WPARAM, LPARAM)</span></span>;</span><br><span class="line"></span><br><span class="line">TCHAR szChildClass[] = TEXT(<span class="string">"CHECK_CHILD"</span>);</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> WINAPI <span class="title">WinMain</span><span class="params">(HINSTANCE hInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	HINSTANCE hPrevInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	PSTR szCmdLine,</span></span></span><br><span class="line"><span class="function"><span class="params">	<span class="keyword">int</span> iCmdShow)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> TCHAR * szAppName = TEXT(<span class="string">"MyWindows"</span>);</span><br><span class="line">	HWND hwnd;            <span class="comment">//句柄，特殊的指针，找到窗口的标识</span></span><br><span class="line">	MSG msg;              <span class="comment">//消息结构，windwos核心就是消息机制</span></span><br><span class="line">	WNDCLASS wndclass;    <span class="comment">//窗口类结构</span></span><br><span class="line"></span><br><span class="line">	wndclass.style = CS_HREDRAW | CS_VREDRAW;                     <span class="comment">//窗口风格, CS_HERDRAW表示水平调整窗口会发生重绘</span></span><br><span class="line">	wndclass.lpfnWndProc = WndProc;                               <span class="comment">//窗口过程</span></span><br><span class="line">	wndclass.cbClsExtra = <span class="number">0</span>;                                      <span class="comment">//预留的额外空间</span></span><br><span class="line">	wndclass.cbWndExtra = <span class="number">0</span>;                                      <span class="comment">//</span></span><br><span class="line">	wndclass.hInstance = hInstance;                               <span class="comment">//实例句柄</span></span><br><span class="line">	wndclass.hIcon = LoadIcon(<span class="literal">NULL</span>, IDI_APPLICATION);             <span class="comment">//图标</span></span><br><span class="line">	wndclass.hCursor = LoadCursor(<span class="literal">NULL</span>, IDC_ARROW);               <span class="comment">//光标</span></span><br><span class="line">	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); <span class="comment">//背景</span></span><br><span class="line">	wndclass.lpszClassName = szAppName;                           <span class="comment">//窗口类的名字</span></span><br><span class="line">	wndclass.lpszMenuName = <span class="literal">NULL</span>;                                 <span class="comment">//菜单名字</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//注册窗口，传入窗口类结构指针</span></span><br><span class="line">	<span class="keyword">if</span> (!RegisterClass(&amp;wndclass)) &#123;<span class="comment">//返回NULL</span></span><br><span class="line">		MessageBox(<span class="literal">NULL</span>, TEXT(<span class="string">"这个改程序需要在Windows NT才能执行"</span>), szAppName, MB_ICONERROR);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//注册子窗口</span></span><br><span class="line">	wndclass.lpfnWndProc = ChildWndProc;</span><br><span class="line">	wndclass.cbWndExtra = <span class="keyword">sizeof</span>(<span class="keyword">long</span>);</span><br><span class="line">	wndclass.hIcon = <span class="literal">NULL</span>;</span><br><span class="line">	wndclass.lpszClassName = szChildClass;</span><br><span class="line"></span><br><span class="line">	RegisterClass(&amp;wndclass);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//创建窗口</span></span><br><span class="line">	hwnd = CreateWindow(szAppName,  <span class="comment">//窗口类名称</span></span><br><span class="line">		TEXT(<span class="string">"中国"</span>),               <span class="comment">//窗口标题</span></span><br><span class="line">		WS_OVERLAPPEDWINDOW,        <span class="comment">//窗口风格</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x方向尺寸</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y方向尺寸</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//父窗口句柄</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//窗口菜单句柄</span></span><br><span class="line">		hInstance,                  <span class="comment">//程序实例句柄</span></span><br><span class="line">		<span class="literal">NULL</span>);                      <span class="comment">//创建参数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示窗口</span></span><br><span class="line">	ShowWindow(hwnd, iCmdShow);</span><br><span class="line">	<span class="comment">//更新窗口</span></span><br><span class="line">	UpdateWindow(hwnd);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//消息循环</span></span><br><span class="line">	<span class="keyword">while</span> (GetMessage(&amp;msg, <span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">0</span>)) &#123;<span class="comment">//WM_QUIT消息，返回0</span></span><br><span class="line">		TranslateMessage(&amp;msg);</span><br><span class="line">		DispatchMessage(&amp;msg);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> msg.wParam;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> HWND hwndChild[DIVISIONS][DIVISIONS];</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> x, y, cxBlock, cyBlock;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_CREATE:</span><br><span class="line">		<span class="keyword">for</span> (x = <span class="number">0</span>; x &lt; DIVISIONS; ++x) &#123;</span><br><span class="line">			<span class="keyword">for</span> (y = <span class="number">0</span>; y &lt; DIVISIONS; ++y) &#123;</span><br><span class="line">				<span class="comment">//创建子窗口句柄</span></span><br><span class="line">				hwndChild[x][y] = CreateWindow(szChildClass,</span><br><span class="line">					<span class="literal">NULL</span>,</span><br><span class="line">					WS_CHILD | WS_VISIBLE,</span><br><span class="line">					<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>,</span><br><span class="line">					hwnd,</span><br><span class="line">					(HMENU)(y &lt;&lt; <span class="number">8</span> | x), <span class="comment">//相同的父窗口中，每个子窗口的id不能相同</span></span><br><span class="line">					(HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE), <span class="comment">//获取窗口的实例句柄</span></span><br><span class="line">					<span class="literal">NULL</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//确定子窗口的位置和尺寸</span></span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		cxBlock = GET_X_LPARAM(lParam) / DIVISIONS;</span><br><span class="line">		cyBlock = GET_Y_LPARAM(lParam) / DIVISIONS;</span><br><span class="line">		<span class="comment">//初始化子窗口大小和位置</span></span><br><span class="line">		<span class="keyword">for</span> (x = <span class="number">0</span>; x &lt; DIVISIONS; ++x) &#123;</span><br><span class="line">			<span class="keyword">for</span> (y = <span class="number">0</span>; y &lt; DIVISIONS; ++y) &#123;</span><br><span class="line">				MoveWindow(hwndChild[x][y], cxBlock*x, cyBlock*y, cxBlock, cyBlock, TRUE);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//点击在非子窗口时</span></span><br><span class="line">	<span class="keyword">case</span> WM_LBUTTONDOWN:</span><br><span class="line">		MessageBeep(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">ChildWndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="comment">//初始化额外参数为0，表示没有被点击</span></span><br><span class="line">	<span class="keyword">case</span> WM_CREATE:</span><br><span class="line">		SetWindowLongPtr(hwnd, <span class="number">0</span>, <span class="number">0</span>);  <span class="comment">//设定子窗口中额外参数的值为0(第二个0)，第一个0表示额外参数的便宜</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//点击该子窗口时，将额外参数的值与1异或</span></span><br><span class="line">	<span class="keyword">case</span> WM_LBUTTONDOWN:</span><br><span class="line">		SetWindowLongPtr(hwnd, <span class="number">0</span>, <span class="number">1</span> ^ GetWindowLongPtr(hwnd, <span class="number">0</span>)); <span class="comment">//1表示被点中</span></span><br><span class="line">		InvalidateRect(hwnd, <span class="literal">NULL</span>, FALSE);</span><br><span class="line">		<span class="keyword">return</span>  <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//绘制子窗口</span></span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		GetClientRect(hwnd, &amp;rect);</span><br><span class="line">		Rectangle(hdc, <span class="number">0</span>, <span class="number">0</span>, rect.right, rect.bottom);</span><br><span class="line">		<span class="keyword">if</span> (GetWindowLongPtr(hwnd, <span class="number">0</span>)) &#123;</span><br><span class="line">			MoveToEx(hdc, <span class="number">0</span>, <span class="number">0</span>, <span class="literal">NULL</span>);</span><br><span class="line">			LineTo(hdc, rect.right, rect.bottom);</span><br><span class="line">			MoveToEx(hdc, <span class="number">0</span>, rect.bottom, <span class="literal">NULL</span>);</span><br><span class="line">			LineTo(hdc, rect.right, <span class="number">0</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>丰富上面的实例，可以键盘在各个子窗口中移动。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windowsx.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> DIVISIONS 5</span></span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND, UINT, WPARAM, LPARAM)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">ChildWndProc</span><span class="params">(HWND, UINT, WPARAM, LPARAM)</span></span>;</span><br><span class="line"></span><br><span class="line">TCHAR szChildClass[] = TEXT(<span class="string">"CHECK_CHILD"</span>); <span class="comment">//子窗口名字</span></span><br><span class="line"><span class="keyword">int</span> idFocus = <span class="number">0</span>;   <span class="comment">//记录当前焦点所在子窗口的id</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> WINAPI <span class="title">WinMain</span><span class="params">(HINSTANCE hInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	HINSTANCE hPrevInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	PSTR szCmdLine,</span></span></span><br><span class="line"><span class="function"><span class="params">	<span class="keyword">int</span> iCmdShow)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> TCHAR * szAppName = TEXT(<span class="string">"MyWindows"</span>);</span><br><span class="line">	HWND hwnd;            <span class="comment">//句柄，特殊的指针，找到窗口的标识</span></span><br><span class="line">	MSG msg;              <span class="comment">//消息结构，windwos核心就是消息机制</span></span><br><span class="line">	WNDCLASS wndclass;    <span class="comment">//窗口类结构</span></span><br><span class="line"></span><br><span class="line">	wndclass.style = CS_HREDRAW | CS_VREDRAW;                     <span class="comment">//窗口风格, CS_HERDRAW表示水平调整窗口会发生重绘</span></span><br><span class="line">	wndclass.lpfnWndProc = WndProc;                               <span class="comment">//窗口过程</span></span><br><span class="line">	wndclass.cbClsExtra = <span class="number">0</span>;                                      <span class="comment">//预留的额外空间</span></span><br><span class="line">	wndclass.cbWndExtra = <span class="number">0</span>;                                      <span class="comment">//</span></span><br><span class="line">	wndclass.hInstance = hInstance;                               <span class="comment">//实例句柄</span></span><br><span class="line">	wndclass.hIcon = LoadIcon(<span class="literal">NULL</span>, IDI_APPLICATION);             <span class="comment">//图标</span></span><br><span class="line">	wndclass.hCursor = LoadCursor(<span class="literal">NULL</span>, IDC_ARROW);               <span class="comment">//光标</span></span><br><span class="line">	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); <span class="comment">//背景</span></span><br><span class="line">	wndclass.lpszClassName = szAppName;                           <span class="comment">//窗口类的名字</span></span><br><span class="line">	wndclass.lpszMenuName = <span class="literal">NULL</span>;                                 <span class="comment">//菜单名字</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//注册窗口，传入窗口类结构指针</span></span><br><span class="line">	<span class="keyword">if</span> (!RegisterClass(&amp;wndclass)) &#123;<span class="comment">//返回NULL</span></span><br><span class="line">		MessageBox(<span class="literal">NULL</span>, TEXT(<span class="string">"这个改程序需要在Windows NT才能执行"</span>), szAppName, MB_ICONERROR);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//注册子窗口</span></span><br><span class="line">	wndclass.lpfnWndProc = ChildWndProc;</span><br><span class="line">	wndclass.cbWndExtra = <span class="keyword">sizeof</span>(<span class="keyword">long</span>);</span><br><span class="line">	wndclass.hIcon = <span class="literal">NULL</span>;</span><br><span class="line">	wndclass.lpszClassName = szChildClass;</span><br><span class="line"></span><br><span class="line">	RegisterClass(&amp;wndclass);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//创建窗口</span></span><br><span class="line">	hwnd = CreateWindow(szAppName,  <span class="comment">//窗口类名称</span></span><br><span class="line">		TEXT(<span class="string">"中国"</span>),               <span class="comment">//窗口标题</span></span><br><span class="line">		WS_OVERLAPPEDWINDOW,        <span class="comment">//窗口风格</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x方向尺寸</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y方向尺寸</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//父窗口句柄</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//窗口菜单句柄</span></span><br><span class="line">		hInstance,                  <span class="comment">//程序实例句柄</span></span><br><span class="line">		<span class="literal">NULL</span>);                      <span class="comment">//创建参数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示窗口</span></span><br><span class="line">	ShowWindow(hwnd, iCmdShow);</span><br><span class="line">	<span class="comment">//更新窗口</span></span><br><span class="line">	UpdateWindow(hwnd);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//消息循环</span></span><br><span class="line">	<span class="keyword">while</span> (GetMessage(&amp;msg, <span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">0</span>)) &#123;<span class="comment">//WM_QUIT消息，返回0</span></span><br><span class="line">		TranslateMessage(&amp;msg);</span><br><span class="line">		DispatchMessage(&amp;msg);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> msg.wParam;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> HWND hwndChild[DIVISIONS][DIVISIONS];</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> x, y, cxBlock, cyBlock;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_CREATE:</span><br><span class="line">		<span class="keyword">for</span> (x = <span class="number">0</span>; x &lt; DIVISIONS; ++x) &#123;</span><br><span class="line">			<span class="keyword">for</span> (y = <span class="number">0</span>; y &lt; DIVISIONS; ++y) &#123;</span><br><span class="line">				<span class="comment">//创建子窗口句柄</span></span><br><span class="line">				hwndChild[x][y] = CreateWindow(szChildClass,</span><br><span class="line">					<span class="literal">NULL</span>,</span><br><span class="line">					WS_CHILD | WS_VISIBLE,</span><br><span class="line">					<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>,</span><br><span class="line">					hwnd,</span><br><span class="line">					(HMENU)(y &lt;&lt; <span class="number">8</span> | x), <span class="comment">//相同的父窗口中，每个子窗口的id不能相同</span></span><br><span class="line">					(HINSTANCE)GetWindowLongPtr(hwnd, GWL_HINSTANCE), <span class="comment">//获取窗口的实例句柄</span></span><br><span class="line">					<span class="literal">NULL</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//确定子窗口的位置和尺寸</span></span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		cxBlock = GET_X_LPARAM(lParam) / DIVISIONS;</span><br><span class="line">		cyBlock = GET_Y_LPARAM(lParam) / DIVISIONS;</span><br><span class="line">		<span class="comment">//初始化子窗口大小和位置</span></span><br><span class="line">		<span class="keyword">for</span> (x = <span class="number">0</span>; x &lt; DIVISIONS; ++x) &#123;</span><br><span class="line">			<span class="keyword">for</span> (y = <span class="number">0</span>; y &lt; DIVISIONS; ++y) &#123;</span><br><span class="line">				MoveWindow(hwndChild[x][y], cxBlock*x, cyBlock*y, cxBlock, cyBlock, TRUE);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//点击在非子窗口时</span></span><br><span class="line">	<span class="keyword">case</span> WM_LBUTTONDOWN:</span><br><span class="line">		MessageBeep(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_SETFOCUS:</span><br><span class="line">		SetFocus(GetDlgItem(hwnd, idFocus)); <span class="comment">//指定id的子窗口句柄</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_KEYDOWN:</span><br><span class="line">		x = idFocus &amp; <span class="number">0xFF</span>;</span><br><span class="line">		y = idFocus &gt;&gt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">switch</span> (wParam) &#123;</span><br><span class="line">		<span class="keyword">case</span> VK_UP:</span><br><span class="line">			--y;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_DOWN:</span><br><span class="line">			++y;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_LEFT:</span><br><span class="line">			--x;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_RIGHT:</span><br><span class="line">			++x;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_HOME:</span><br><span class="line">			x = y = <span class="number">0</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_END:</span><br><span class="line">			x = y = DIVISIONS - <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		x = (x + DIVISIONS) % DIVISIONS;</span><br><span class="line">		y = (y + DIVISIONS) % DIVISIONS;</span><br><span class="line"></span><br><span class="line">		idFocus = y &lt;&lt; <span class="number">8</span> | x;</span><br><span class="line"></span><br><span class="line">		SetFocus(GetDlgItem(hwnd, idFocus));</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">ChildWndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="comment">//初始化额外参数为0，表示没有被点击</span></span><br><span class="line">	<span class="keyword">case</span> WM_CREATE:</span><br><span class="line">		SetWindowLongPtr(hwnd, <span class="number">0</span>, <span class="number">0</span>);  <span class="comment">//设定子窗口中额外参数的值为0(第二个0)，第一个0表示额外参数的便宜</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_KEYDOWN:</span><br><span class="line">		<span class="keyword">if</span> (wParam != VK_RETURN &amp;&amp; wParam != VK_SPACE) &#123; <span class="comment">//如果按下的不是回车和空格键，给主窗口发送消息，让主程序处理上下左右操作</span></span><br><span class="line">			SendMessage(GetParent(hwnd), message, wParam, lParam);</span><br><span class="line">			<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//没有return</span></span><br><span class="line">		<span class="comment">//如果是回车和空格键，则直接给当前的子窗口获得焦点，并将额外参数设定为被点中</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//点击该子窗口时，将额外参数的值与1异或</span></span><br><span class="line">	<span class="keyword">case</span> WM_LBUTTONDOWN:</span><br><span class="line">		SetWindowLongPtr(hwnd, <span class="number">0</span>, <span class="number">1</span> ^ GetWindowLongPtr(hwnd, <span class="number">0</span>)); <span class="comment">//1表示被点中</span></span><br><span class="line">		SetFocus(hwnd);</span><br><span class="line">		InvalidateRect(hwnd, <span class="literal">NULL</span>, FALSE);</span><br><span class="line">		<span class="keyword">return</span>  <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_SETFOCUS:<span class="comment">//当前子窗口获得焦点，将idFocus设定为在子窗口的id，同时重绘子窗口，通过不要return来实现</span></span><br><span class="line">		idFocus = GetWindowLongPtr(hwnd, GWL_ID);</span><br><span class="line">		<span class="comment">//不要return</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_KILLFOCUS:</span><br><span class="line">		InvalidateRect(hwnd, <span class="literal">NULL</span>, TRUE);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//绘制子窗口</span></span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		GetClientRect(hwnd, &amp;rect);</span><br><span class="line">		Rectangle(hdc, <span class="number">0</span>, <span class="number">0</span>, rect.right, rect.bottom);</span><br><span class="line">		<span class="keyword">if</span> (GetWindowLongPtr(hwnd, <span class="number">0</span>)) &#123;</span><br><span class="line">			MoveToEx(hdc, <span class="number">0</span>, <span class="number">0</span>, <span class="literal">NULL</span>);</span><br><span class="line">			LineTo(hdc, rect.right, rect.bottom);</span><br><span class="line">			MoveToEx(hdc, <span class="number">0</span>, rect.bottom, <span class="literal">NULL</span>);</span><br><span class="line">			LineTo(hdc, rect.right, <span class="number">0</span>);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//给获得焦点的子窗口绘制一个小虚线窗口</span></span><br><span class="line">		<span class="keyword">if</span> (hwnd == GetFocus()) &#123;</span><br><span class="line">			rect.left += rect.right / <span class="number">10</span>;</span><br><span class="line">			rect.right -= rect.left;</span><br><span class="line">			rect.top += rect.bottom / <span class="number">10</span>;</span><br><span class="line">			rect.bottom -= rect.top;</span><br><span class="line"></span><br><span class="line">			SelectObject(hdc, GetStockObject(NULL_BRUSH));</span><br><span class="line">			SelectObject(hdc, CreatePen(PS_DASH, <span class="number">0</span>, <span class="number">0</span>));</span><br><span class="line">			Rectangle(hdc, rect.left, rect.top, rect.right, rect.bottom);</span><br><span class="line">			DeleteObject(SelectObject(hdc, GetStockObject(BLACK_PEN)));</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>程序运行结果：</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200506223947680.png" alt="image-20200506223947680"></p>
<h1 id="捕获鼠标"><a href="#捕获鼠标" class="headerlink" title="捕获鼠标"></a>捕获鼠标</h1><p>如何捕获在窗口之外的鼠标消息。</p>
<p>通过<code>SetCapture(hwnd)</code>来捕获，通过<code>ReleaseCapture()</code>来释放</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;windows.h&gt;</span><br><span class="line">#include &lt;windowsx.h&gt;</span><br><span class="line"></span><br><span class="line">LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);</span><br><span class="line">void DrawBoxOutline(HWND hwnd, POINT ptBeg, POINT ptEnd);</span><br><span class="line"></span><br><span class="line">int WINAPI WinMain(HINSTANCE hInstance,</span><br><span class="line">	HINSTANCE hPrevInstance,</span><br><span class="line">	PSTR szCmdLine,</span><br><span class="line">	int iCmdShow) &#123;</span><br><span class="line"></span><br><span class="line">	static TCHAR * szAppName = TEXT(&quot;MyWindows&quot;);</span><br><span class="line">	HWND hwnd;            //句柄，特殊的指针，找到窗口的标识</span><br><span class="line">	MSG msg;              //消息结构，windwos核心就是消息机制</span><br><span class="line">	WNDCLASS wndclass;    //窗口类结构</span><br><span class="line"></span><br><span class="line">	wndclass.style = CS_HREDRAW | CS_VREDRAW;                     //窗口风格, CS_HERDRAW表示水平调整窗口会发生重绘</span><br><span class="line">	wndclass.lpfnWndProc = WndProc;                               //窗口过程</span><br><span class="line">	wndclass.cbClsExtra = 0;                                      //预留的额外空间</span><br><span class="line">	wndclass.cbWndExtra = 0;                                      //</span><br><span class="line">	wndclass.hInstance = hInstance;                               //实例句柄</span><br><span class="line">	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);             //图标</span><br><span class="line">	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);               //光标</span><br><span class="line">	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //背景</span><br><span class="line">	wndclass.lpszClassName = szAppName;                           //窗口类的名字</span><br><span class="line">	wndclass.lpszMenuName = NULL;                                 //菜单名字</span><br><span class="line"></span><br><span class="line">	//注册窗口，传入窗口类结构指针</span><br><span class="line">	if (!RegisterClass(&amp;wndclass)) &#123;//返回NULL</span><br><span class="line">		MessageBox(NULL, TEXT(&quot;这个改程序需要在Windows NT才能执行&quot;), szAppName, MB_ICONERROR);</span><br><span class="line">		return 0;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	//创建窗口</span><br><span class="line">	hwnd = CreateWindow(szAppName,  //窗口类名称</span><br><span class="line">		TEXT(&quot;中国&quot;),               //窗口标题</span><br><span class="line">		WS_OVERLAPPEDWINDOW,        //窗口风格</span><br><span class="line">		CW_USEDEFAULT,              //初始x坐标</span><br><span class="line">		CW_USEDEFAULT,              //初始y坐标</span><br><span class="line">		CW_USEDEFAULT,              //初始x方向尺寸</span><br><span class="line">		CW_USEDEFAULT,              //初始y方向尺寸</span><br><span class="line">		NULL,                       //父窗口句柄</span><br><span class="line">		NULL,                       //窗口菜单句柄</span><br><span class="line">		hInstance,                  //程序实例句柄</span><br><span class="line">		NULL);                      //创建参数</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	//显示窗口</span><br><span class="line">	ShowWindow(hwnd, iCmdShow);</span><br><span class="line">	//更新窗口</span><br><span class="line">	UpdateWindow(hwnd);</span><br><span class="line"></span><br><span class="line">	//消息循环</span><br><span class="line">	while (GetMessage(&amp;msg, NULL, 0, 0)) &#123;//WM_QUIT消息，返回0</span><br><span class="line">		TranslateMessage(&amp;msg);</span><br><span class="line">		DispatchMessage(&amp;msg);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	return msg.wParam;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">//绘制鼠标移动时的透明矩形</span><br><span class="line">void DrawBoxOutline(HWND hwnd, POINT ptBeg, POINT ptEnd) &#123;</span><br><span class="line">	HDC hdc = GetDC(hwnd);</span><br><span class="line"></span><br><span class="line">	SetROP2(hdc, R2_NOT);                  //将画笔的颜色进行取反，为了实现删除之前绘制的透明矩形的</span><br><span class="line">	SelectObject(hdc, GetStockObject(NULL_BRUSH));</span><br><span class="line">	Rectangle(hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y);</span><br><span class="line"></span><br><span class="line">	ReleaseDC(hwnd, hdc);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) &#123;</span><br><span class="line">	static BOOL fBlocking, fValidBox; //fBlocking表示鼠标已经点下，还未松开； fValidBox表示当前已经绘制了一个黑色矩形</span><br><span class="line">	static POINT ptBeg, ptEnd, ptBoxBeg, ptBoxEnd; //ptBeg和ptBeg表示鼠标移动时绘制的矩形，另外两个表示鼠标释放时绘制的黑色矩形</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT ps;</span><br><span class="line"></span><br><span class="line">	switch (message) &#123;</span><br><span class="line">	case WM_LBUTTONDOWN:   //鼠标点下时</span><br><span class="line">		ptBeg.x = ptEnd.x = GET_X_LPARAM(lParam);</span><br><span class="line">		ptBeg.y = ptEnd.y = GET_Y_LPARAM(lParam);</span><br><span class="line"></span><br><span class="line">		SetCapture(hwnd);   //捕获窗口外的鼠标，只能是当前线程</span><br><span class="line">		DrawBoxOutline(hwnd, ptBeg, ptEnd);      </span><br><span class="line">		SetCursor(LoadCursor(NULL, IDC_CROSS));</span><br><span class="line"></span><br><span class="line">		fBlocking = TRUE;</span><br><span class="line">		return 0;</span><br><span class="line"></span><br><span class="line">	case WM_MOUSEMOVE:  //鼠标移动时</span><br><span class="line">		if (fBlocking == TRUE) &#123;</span><br><span class="line">			DrawBoxOutline(hwnd, ptBeg, ptEnd);    //用相反的颜色再画一次，即去掉现在的边框</span><br><span class="line">			SetCursor(LoadCursor(NULL, IDC_CROSS)); </span><br><span class="line"></span><br><span class="line">			ptEnd.x = GET_X_LPARAM(lParam);</span><br><span class="line">			ptEnd.y = GET_Y_LPARAM(lParam);</span><br><span class="line"></span><br><span class="line">			DrawBoxOutline(hwnd, ptBeg, ptEnd);  //用再次相反的颜色，重新画一个边框</span><br><span class="line">		&#125;</span><br><span class="line">		return 0;</span><br><span class="line"></span><br><span class="line">	case WM_LBUTTONUP:   //鼠标释放时</span><br><span class="line">		if (fBlocking == TRUE) &#123;</span><br><span class="line">			DrawBoxOutline(hwnd, ptBeg, ptEnd);</span><br><span class="line"></span><br><span class="line">			ptBoxBeg = ptBeg;</span><br><span class="line">			ptBoxEnd.x = GET_X_LPARAM(lParam);</span><br><span class="line">			ptBoxEnd.y = GET_Y_LPARAM(lParam);</span><br><span class="line"></span><br><span class="line">			ReleaseCapture();    //释放窗口的鼠标捕获</span><br><span class="line">			SetCursor(LoadCursor(NULL, IDC_ARROW));</span><br><span class="line">			fBlocking = FALSE;</span><br><span class="line">			</span><br><span class="line">			fValidBox = TRUE;</span><br><span class="line"></span><br><span class="line">			InvalidateRect(hwnd, NULL, TRUE);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">	case WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line"></span><br><span class="line">		//绘制鼠标释放时的黑色矩形</span><br><span class="line">		if (fValidBox == TRUE) &#123;</span><br><span class="line">			SelectObject(hdc, GetStockObject(BLACK_BRUSH));</span><br><span class="line">			Rectangle(hdc, ptBoxBeg.x, ptBoxBeg.y, ptBoxEnd.x, ptBoxEnd.y);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		if (fBlocking) &#123;</span><br><span class="line">			SetROP2(hdc, R2_NOT);</span><br><span class="line">			SelectObject(hdc, GetStockObject(NULL_BRUSH));</span><br><span class="line">			Rectangle(hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		return 0;</span><br><span class="line"></span><br><span class="line">	case WM_DESTROY:</span><br><span class="line">		PostQuitMessage(0);</span><br><span class="line">		return 0;</span><br><span class="line">	&#125;</span><br><span class="line">	return DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="鼠标滚轮"><a href="#鼠标滚轮" class="headerlink" title="鼠标滚轮"></a>鼠标滚轮</h1><p>利用鼠标滚轮实现滚动条的移动。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;strsafe.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"SysMets.h"</span></span></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND, UINT, WPARAM, LPARAM)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> WINAPI <span class="title">WinMain</span><span class="params">(HINSTANCE hInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	HINSTANCE hPrevInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	PSTR szCmdLine,</span></span></span><br><span class="line"><span class="function"><span class="params">	<span class="keyword">int</span> iCmdShow)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> TCHAR * szAppName = TEXT(<span class="string">"MyWindows"</span>);</span><br><span class="line">	HWND hwnd;            <span class="comment">//句柄，特殊的指针，找到窗口的标识</span></span><br><span class="line">	MSG msg;              <span class="comment">//消息结构，windwos核心就是消息机制</span></span><br><span class="line">	WNDCLASS wndclass;    <span class="comment">//窗口类结构</span></span><br><span class="line"></span><br><span class="line">	wndclass.style = CS_HREDRAW | CS_VREDRAW;                     <span class="comment">//窗口风格, CS_HERDRAW表示水平调整窗口会发生重绘</span></span><br><span class="line">	wndclass.lpfnWndProc = WndProc;                               <span class="comment">//窗口过程</span></span><br><span class="line">	wndclass.cbClsExtra = <span class="number">0</span>;                                      <span class="comment">//预留的额外空间</span></span><br><span class="line">	wndclass.cbWndExtra = <span class="number">0</span>;                                      <span class="comment">//</span></span><br><span class="line">	wndclass.hInstance = hInstance;                               <span class="comment">//实例句柄</span></span><br><span class="line">	wndclass.hIcon = LoadIcon(<span class="literal">NULL</span>, IDI_APPLICATION);             <span class="comment">//图标</span></span><br><span class="line">	wndclass.hCursor = LoadCursor(<span class="literal">NULL</span>, IDC_ARROW);               <span class="comment">//光标</span></span><br><span class="line">	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); <span class="comment">//背景</span></span><br><span class="line">	wndclass.lpszClassName = szAppName;                           <span class="comment">//窗口类的名字</span></span><br><span class="line">	wndclass.lpszMenuName = <span class="literal">NULL</span>;                                 <span class="comment">//菜单名字</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//注册窗口，传入窗口类结构指针</span></span><br><span class="line">	<span class="keyword">if</span> (!RegisterClass(&amp;wndclass)) &#123;<span class="comment">//返回NULL</span></span><br><span class="line">		MessageBox(<span class="literal">NULL</span>, TEXT(<span class="string">"这个改程序需要在Windows NT才能执行"</span>), szAppName, MB_ICONERROR);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//创建窗口</span></span><br><span class="line">	hwnd = CreateWindow(szAppName,  <span class="comment">//窗口类名称</span></span><br><span class="line">		TEXT(<span class="string">"中国"</span>),               <span class="comment">//窗口标题</span></span><br><span class="line">		WS_OVERLAPPEDWINDOW | WS_VSCROLL,        <span class="comment">//窗口风格</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x方向尺寸</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y方向尺寸</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//父窗口句柄</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//窗口菜单句柄</span></span><br><span class="line">		hInstance,                  <span class="comment">//程序实例句柄</span></span><br><span class="line">		<span class="literal">NULL</span>);                      <span class="comment">//创建参数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示窗口</span></span><br><span class="line">	ShowWindow(hwnd, iCmdShow);</span><br><span class="line">	<span class="comment">//更新窗口</span></span><br><span class="line">	UpdateWindow(hwnd);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//消息循环</span></span><br><span class="line">	<span class="keyword">while</span> (GetMessage(&amp;msg, <span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">0</span>)) &#123;<span class="comment">//WM_QUIT消息，返回0</span></span><br><span class="line">		TranslateMessage(&amp;msg);</span><br><span class="line">		DispatchMessage(&amp;msg);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> msg.wParam;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line"></span><br><span class="line">	TCHAR szBuffer[<span class="number">10</span>];</span><br><span class="line">	<span class="keyword">size_t</span> iTarget;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxChar, cyChar, cxCaps;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> cxClient, cyClient, iVscrollPos;</span><br><span class="line">	TEXTMETRIC tm;</span><br><span class="line">	<span class="keyword">int</span> i, y;</span><br><span class="line"></span><br><span class="line">	SCROLLINFO si;</span><br><span class="line">	<span class="keyword">int</span> yPos, FirstLine, LastLine;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> iDeltaPerLine, iAccumDelta; <span class="comment">//iDeltaPerLine表示滚动一行Delta增加的值</span></span><br><span class="line">	ULONG ulScrollLines;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_CREATE:</span><br><span class="line">		hdc = GetDC(hwnd);</span><br><span class="line">		GetTextMetrics(hdc, &amp;tm);</span><br><span class="line">		cxChar = tm.tmAveCharWidth;     <span class="comment">//字符平均宽度</span></span><br><span class="line">		cxCaps = (tm.tmPitchAndFamily &amp; <span class="number">1</span> ? <span class="number">3</span> : <span class="number">2</span>) * cxChar / <span class="number">2</span>; <span class="comment">//如果低位为0,表示等宽字体，如果为1,表示变宽字体，大写字母是小写字母的1.5倍</span></span><br><span class="line">		cyChar = tm.tmHeight + tm.tmExternalLeading;</span><br><span class="line"></span><br><span class="line">		ReleaseDC(hwnd, hdc);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//没有return</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//设置改变或初始化时，计算每行的delta的值</span></span><br><span class="line">	<span class="keyword">case</span> WM_SETTINGCHANGE:</span><br><span class="line">		SystemParametersInfo(SPI_GETWHEELSCROLLLINES, <span class="number">0</span>, &amp;ulScrollLines, <span class="number">0</span>);</span><br><span class="line">		<span class="keyword">if</span> (ulScrollLines) &#123;</span><br><span class="line">			iDeltaPerLine = WHEEL_DELTA / ulScrollLines; <span class="comment">//每行的detal</span></span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			iDeltaPerLine = <span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_SIZE:</span><br><span class="line">		hdc = GetDC(hwnd);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//设置滚动条，包括范围和页面大小</span></span><br><span class="line">		si.cbSize = <span class="keyword">sizeof</span>(si);</span><br><span class="line">		si.fMask = SIF_RANGE | SIF_PAGE;</span><br><span class="line">		si.nMax = NUMLINES<span class="number">-1</span>;</span><br><span class="line">		si.nMin = <span class="number">0</span>;</span><br><span class="line">		si.nPage = cyClient / cyChar;</span><br><span class="line">		SetScrollInfo(hwnd, SB_VERT, &amp;si, TRUE);</span><br><span class="line"></span><br><span class="line">		ReleaseDC(hwnd, hdc);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//响应鼠标滚轮操作</span></span><br><span class="line">	<span class="keyword">case</span> WM_MOUSEWHEEL:</span><br><span class="line">		<span class="keyword">if</span> (iDeltaPerLine == <span class="number">0</span>) &#123; <span class="comment">//如果每行的delta值为0，表示不支持滚轮</span></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		iAccumDelta = GET_WHEEL_DELTA_WPARAM(wParam);  <span class="comment">//得到滚动的delta值</span></span><br><span class="line"></span><br><span class="line">		<span class="keyword">while</span> (iAccumDelta &gt;= iDeltaPerLine) &#123;    <span class="comment">//向上滚动</span></span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, <span class="number">0</span>);</span><br><span class="line">			iAccumDelta -= iDeltaPerLine;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">while</span> (iAccumDelta &lt;= -iDeltaPerLine) &#123;   <span class="comment">//向下滚动</span></span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, <span class="number">0</span>);</span><br><span class="line">			iAccumDelta += iDeltaPerLine;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;		</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//获取滚动条位置</span></span><br><span class="line">		si.cbSize = <span class="keyword">sizeof</span>(si);</span><br><span class="line">		si.fMask = SIF_POS;</span><br><span class="line">		GetScrollInfo(hwnd, SB_VERT, &amp;si);</span><br><span class="line">		yPos = si.nPos;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//计算需要重绘的区域</span></span><br><span class="line">		FirstLine = max(<span class="number">0</span>, yPos + ps.rcPaint.top / cyChar);</span><br><span class="line">		LastLine = min(NUMLINES<span class="number">-1</span>, yPos + ps.rcPaint.bottom / cyChar);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//重绘失效区域</span></span><br><span class="line">		<span class="keyword">for</span> (i = FirstLine; i &lt;= LastLine; ++i) &#123;</span><br><span class="line">			y = cyChar * (i - yPos);</span><br><span class="line">			StringCchLength(sysMets[i].szContent, <span class="number">128</span>, &amp;iTarget);</span><br><span class="line">			TextOut(hdc, <span class="number">0</span>, y, sysMets[i].szContent, iTarget);</span><br><span class="line"></span><br><span class="line">			StringCchLength(sysMets[i].szDes, <span class="number">128</span>, &amp;iTarget);</span><br><span class="line">			TextOut(hdc, <span class="number">22</span> * cxCaps, y, sysMets[i].szDes, iTarget);</span><br><span class="line"></span><br><span class="line">			SetTextAlign(hdc, TA_TOP | TA_RIGHT);</span><br><span class="line">			StringCchPrintf(szBuffer, <span class="number">10</span>, TEXT(<span class="string">"%d"</span>), GetSystemMetrics(sysMets[i].iIndex));</span><br><span class="line">			StringCchLength(szBuffer, <span class="number">10</span>, &amp;iTarget);</span><br><span class="line">			TextOut(hdc, <span class="number">22</span> * cxCaps + <span class="number">40</span> * cxChar, y, szBuffer, iTarget);</span><br><span class="line"></span><br><span class="line">			SetTextAlign(hdc, TA_TOP | TA_LEFT);</span><br><span class="line"></span><br><span class="line">		&#125;</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_VSCROLL:</span><br><span class="line">		<span class="comment">//获取滚动条的位置</span></span><br><span class="line">		si.cbSize = <span class="keyword">sizeof</span>(si);</span><br><span class="line">		si.fMask = SIF_ALL;</span><br><span class="line">		GetScrollInfo(hwnd, SB_VERT, &amp;si);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//记录当前滚动条位置，以为了后续判断是否滚动条位置发生变化</span></span><br><span class="line">		yPos = si.nPos;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//响应各种操作</span></span><br><span class="line">		<span class="keyword">switch</span> (LOWORD(wParam))</span><br><span class="line">		&#123;</span><br><span class="line">		<span class="keyword">case</span> SB_TOP:</span><br><span class="line">			si.nPos = si.nMin;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_BOTTOM:</span><br><span class="line">			si.nPos = si.nMax;</span><br><span class="line">		<span class="keyword">case</span> SB_LINEUP:</span><br><span class="line">			si.nPos -= <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_LINEDOWN:</span><br><span class="line">			si.nPos += <span class="number">1</span>;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_PAGEUP:</span><br><span class="line">			si.nPos -= si.nPage;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_PAGEDOWN:</span><br><span class="line">			si.nPos += si.nPage;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> SB_THUMBTRACK:</span><br><span class="line">			si.nPos = si.nTrackPos;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//设置滚动条位置</span></span><br><span class="line">		si.fMask = SIF_POS;</span><br><span class="line">		SetScrollInfo(hwnd, SB_VERT, &amp;si, TRUE);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//重新获取滚动条位置</span></span><br><span class="line">		GetScrollInfo(hwnd, SB_VERT, &amp;si);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//判断滚动条位置是否发生变化</span></span><br><span class="line">		<span class="keyword">if</span> (yPos != si.nPos) &#123;</span><br><span class="line">			<span class="comment">//滚动客户区的内容</span></span><br><span class="line">			ScrollWindow(hwnd, <span class="number">0</span>, cyChar * (yPos - si.nPos), <span class="literal">NULL</span>, <span class="literal">NULL</span>);</span><br><span class="line">			<span class="comment">//发生WM_PAINT消息，重新绘制失效区域</span></span><br><span class="line">			UpdateWindow(hwnd);</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_KEYDOWN:</span><br><span class="line">		<span class="keyword">switch</span> (LOWORD(wParam)) &#123;</span><br><span class="line">		<span class="keyword">case</span> VK_UP:</span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, <span class="number">0</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_DOWN:</span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, <span class="number">0</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_PRIOR:</span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, <span class="number">0</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_NEXT:</span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, <span class="number">0</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_HOME:</span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_TOP, <span class="number">0</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> VK_END:</span><br><span class="line">			SendMessage(hwnd, WM_VSCROLL, SB_BOTTOM, <span class="number">0</span>);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="计时器"><a href="#计时器" class="headerlink" title="计时器"></a>计时器</h1><p>实现计时器，需要使用<code>SetTimer</code>一个函数，通常情况下，在指定时间周期一道，系统就会发送一个缴<code>WM_TIMER</code>的消息通知指定的应用程序。当程序不再需要计时器时，使用<code>KillTimer</code>函数来清除一个计时器。</p>
<p>如果程序在整个运行过程中需要一个计时器，在WinMain函数中或处理<code>WM_Create</code>消息时，调用<code>SetTimer</code>函数；在离开<code>WinMain</code>函数时 或处理<code>WM_DESTROY</code>消息时，调用<code>KillTimer</code>函数。</p>
<p>三种方法实现计时器：</p>
<p>第一种：定时发送wM_TIMER消息</p>
<p>第二种：定时调用计时器回调函数</p>
<p>第三种：让系统自动分配计时器ID</p>
<p>第一种：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">int</span> ID_TIMER = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_CREATE:</span><br><span class="line">		SetTimer(hwnd, ID_TIMER, <span class="number">1000</span>, <span class="literal">NULL</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_TIMER:</span><br><span class="line">		SetClassLongPtr(hwnd, GCLP_HBRBACKGROUND, </span><br><span class="line">			(LONG)(CreateSolidBrush(RGB(rand() % <span class="number">256</span>, rand() % <span class="number">256</span>, rand() % <span class="number">256</span>))));</span><br><span class="line">		InvalidateRect(hwnd, <span class="literal">NULL</span>, TRUE);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		GetClientRect(hwnd, &amp;rect);</span><br><span class="line">		DrawText(hdc, TEXT(<span class="string">"大家好，这是我的点第一个窗口程序！"</span>), <span class="number">-1</span>, &amp;rect,</span><br><span class="line">			DT_SINGLELINE | DT_CENTER | DT_VCENTER);</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		KillTimer(hwnd, ID_TIMER);</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>第二种：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> CALLBACK <span class="title">TimerProc</span><span class="params">(HWND, UINT, UINT, DWORD)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">int</span> ID_TIMER = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_CREATE:</span><br><span class="line">		SetTimer(hwnd, ID_TIMER, <span class="number">1000</span>, TimerProc);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		GetClientRect(hwnd, &amp;rect);</span><br><span class="line">		DrawText(hdc, TEXT(<span class="string">"大家好，这是我的点第一个窗口程序！"</span>), <span class="number">-1</span>, &amp;rect,</span><br><span class="line">			DT_SINGLELINE | DT_CENTER | DT_VCENTER);</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		KillTimer(hwnd, ID_TIMER);</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> CALLBACK <span class="title">TimerProc</span><span class="params">(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)</span> </span>&#123;</span><br><span class="line">	SetClassLongPtr(hwnd, GCLP_HBRBACKGROUND,</span><br><span class="line">		(LONG)(CreateSolidBrush(RGB(rand() % <span class="number">256</span>, rand() % <span class="number">256</span>, rand() % <span class="number">256</span>))));</span><br><span class="line">	InvalidateRect(hwnd, <span class="literal">NULL</span>, TRUE);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>第三种：</p>
<p>将SetTimer的hwnd的值设定为NULL，同时nIDEvent参数指定为一个已经存在的定时器，那么这个定时器将和一个已经存在的非NULL的hWind值的定时器一样被替换。此时nIDEvent会被忽略，并将重新产生一个新的定时器ID。适用于程序需要创建很多计时器，不想去声明每个ID。</p>
<p>同时KillTimer的hwnd的值也设定为NULL。</p>
<p>可以修改一个计时器。方法如下：</p>
<p>将hwnd不为NULL，并且hwnd指定的窗口已经有一个具有nIDEvent值的定时器，则已经存在的定时器会被重新产生的定时器替换。注意，前面两个参数一样，后面的参数需要不一样。</p>
<h1 id="各种按钮"><a href="#各种按钮" class="headerlink" title="各种按钮"></a>各种按钮</h1><p>按钮类</p>
<p> 复选框</p>
<p>编辑框</p>
<p>列表框</p>
<p>组合框</p>
<p>文本字符串</p>
<p>滚动条</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">	<span class="keyword">int</span> iStyle;        </span><br><span class="line">	TCHAR * scText; </span><br><span class="line">&#125; button[] = &#123;</span><br><span class="line">	BS_PUSHBUTTON, TEXT(<span class="string">"PushButton"</span>),</span><br><span class="line">	BS_DEFPUSHBUTTON, TEXT(<span class="string">"DefPushButton"</span>),</span><br><span class="line">	BS_CHECKBOX, TEXT(<span class="string">"CheckBox"</span>),</span><br><span class="line">	BS_AUTOCHECKBOX, TEXT(<span class="string">"AutoCheckBox"</span>),</span><br><span class="line">	BS_RADIOBUTTON, TEXT(<span class="string">"RadioButton"</span>),</span><br><span class="line">	BS_3STATE, TEXT(<span class="string">"3State"</span>),</span><br><span class="line">	BS_AUTO3STATE, TEXT(<span class="string">"Auto3State"</span>),</span><br><span class="line">	BS_GROUPBOX, TEXT(<span class="string">"GroupBox"</span>),</span><br><span class="line">	BS_AUTORADIOBUTTON, TEXT(<span class="string">"AutoRadioButton"</span>),</span><br><span class="line">	BS_OWNERDRAW, TEXT(<span class="string">"Ownerdarw"</span>)</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> NUM (sizeof(button) / sizeof(button[0]))</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND, UINT, WPARAM, LPARAM)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> WINAPI <span class="title">WinMain</span><span class="params">(HINSTANCE hInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	HINSTANCE hPrevInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	PSTR szCmdLine,</span></span></span><br><span class="line"><span class="function"><span class="params">	<span class="keyword">int</span> iCmdShow)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> TCHAR * szAppName = TEXT(<span class="string">"MyWindows"</span>);</span><br><span class="line">	HWND hwnd;            <span class="comment">//句柄，特殊的指针，找到窗口的标识</span></span><br><span class="line">	MSG msg;              <span class="comment">//消息结构，windwos核心就是消息机制</span></span><br><span class="line">	WNDCLASS wndclass;    <span class="comment">//窗口类结构</span></span><br><span class="line"></span><br><span class="line">	wndclass.style = CS_HREDRAW | CS_VREDRAW;                     <span class="comment">//窗口风格, CS_HERDRAW表示水平调整窗口会发生重绘</span></span><br><span class="line">	wndclass.lpfnWndProc = WndProc;                               <span class="comment">//窗口过程</span></span><br><span class="line">	wndclass.cbClsExtra = <span class="number">0</span>;                                      <span class="comment">//预留的额外空间</span></span><br><span class="line">	wndclass.cbWndExtra = <span class="number">0</span>;                                      <span class="comment">//</span></span><br><span class="line">	wndclass.hInstance = hInstance;                               <span class="comment">//实例句柄</span></span><br><span class="line">	wndclass.hIcon = LoadIcon(<span class="literal">NULL</span>, IDI_APPLICATION);             <span class="comment">//图标</span></span><br><span class="line">	wndclass.hCursor = LoadCursor(<span class="literal">NULL</span>, IDC_ARROW);               <span class="comment">//光标</span></span><br><span class="line">	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); <span class="comment">//背景</span></span><br><span class="line">	wndclass.lpszClassName = szAppName;                           <span class="comment">//窗口类的名字</span></span><br><span class="line">	wndclass.lpszMenuName = <span class="literal">NULL</span>;                                 <span class="comment">//菜单名字</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//注册窗口，传入窗口类结构指针</span></span><br><span class="line">	<span class="keyword">if</span> (!RegisterClass(&amp;wndclass)) &#123;<span class="comment">//返回NULL</span></span><br><span class="line">		MessageBox(<span class="literal">NULL</span>, TEXT(<span class="string">"这个改程序需要在Windows NT才能执行"</span>), szAppName, MB_ICONERROR);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//创建窗口</span></span><br><span class="line">	hwnd = CreateWindow(szAppName,  <span class="comment">//窗口类名称</span></span><br><span class="line">		TEXT(<span class="string">"中国"</span>),               <span class="comment">//窗口标题</span></span><br><span class="line">		WS_OVERLAPPEDWINDOW,        <span class="comment">//窗口风格</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x方向尺寸</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y方向尺寸</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//父窗口句柄</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//窗口菜单句柄</span></span><br><span class="line">		hInstance,                  <span class="comment">//程序实例句柄</span></span><br><span class="line">		<span class="literal">NULL</span>);                      <span class="comment">//创建参数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示窗口</span></span><br><span class="line">	ShowWindow(hwnd, iCmdShow);</span><br><span class="line">	<span class="comment">//更新窗口</span></span><br><span class="line">	UpdateWindow(hwnd);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//消息循环</span></span><br><span class="line">	<span class="keyword">while</span> (GetMessage(&amp;msg, <span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">0</span>)) &#123;<span class="comment">//WM_QUIT消息，返回0</span></span><br><span class="line">		TranslateMessage(&amp;msg);</span><br><span class="line">		DispatchMessage(&amp;msg);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> msg.wParam;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span>  cxChar, cyChar;</span><br><span class="line">	<span class="keyword">int</span> i;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_CREATE:</span><br><span class="line">		cxChar = LOWORD(GetDialogBaseUnits()); <span class="comment">//GetDialogBaseUnits()返回的值就是字体的大小，低16位为宽</span></span><br><span class="line">		cyChar = HIWORD(GetDialogBaseUnits()); <span class="comment">//高16为高</span></span><br><span class="line"></span><br><span class="line">		<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; NUM; ++i) &#123;</span><br><span class="line"></span><br><span class="line">			CreateWindow(TEXT(<span class="string">"Button"</span>),</span><br><span class="line">				button[i].scText,</span><br><span class="line">				WS_CHILD | WS_VISIBLE | button[i].iStyle,</span><br><span class="line">				cxChar, cyChar * (<span class="number">1</span> + <span class="number">2</span> *i),  <span class="comment">//位置</span></span><br><span class="line">				<span class="number">20</span>*cxChar, <span class="number">7</span> * cyChar / <span class="number">4</span>, <span class="comment">//大小</span></span><br><span class="line">				hwnd,     <span class="comment">//父窗口句柄</span></span><br><span class="line">				(HMENU)<span class="number">1</span>,</span><br><span class="line">				((LPCREATESTRUCT)lParam)-&gt;hInstance,</span><br><span class="line">				<span class="literal">NULL</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_COMMAND:</span><br><span class="line">		MessageBox(hwnd, TEXT(<span class="string">"点击了PushButton按钮"</span>), TEXT(<span class="string">"提醒"</span>), MB_OK);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_PAINT:</span><br><span class="line">		hdc = BeginPaint(hwnd, &amp;ps);</span><br><span class="line">		GetClientRect(hwnd, &amp;rect);</span><br><span class="line">		DrawText(hdc, TEXT(<span class="string">"大家好，这是我的点第一个窗口程序！"</span>), <span class="number">-1</span>, &amp;rect,</span><br><span class="line">			DT_SINGLELINE | DT_CENTER | DT_VCENTER);</span><br><span class="line">		EndPaint(hwnd, &amp;ps);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>实现翻牌子程序：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;time.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> NUM 5</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> GROUPBUTTONID (NUM+1)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> PUSHBUTTON1 (NUM+2)</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> PUSHBUTON2 (NUM+3)</span></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND, UINT, WPARAM, LPARAM)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> WINAPI <span class="title">WinMain</span><span class="params">(HINSTANCE hInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	HINSTANCE hPrevInstance,</span></span></span><br><span class="line"><span class="function"><span class="params">	PSTR szCmdLine,</span></span></span><br><span class="line"><span class="function"><span class="params">	<span class="keyword">int</span> iCmdShow)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">static</span> TCHAR * szAppName = TEXT(<span class="string">"MyWindows"</span>);</span><br><span class="line">	HWND hwnd;            <span class="comment">//句柄，特殊的指针，找到窗口的标识</span></span><br><span class="line">	MSG msg;              <span class="comment">//消息结构，windwos核心就是消息机制</span></span><br><span class="line">	WNDCLASS wndclass;    <span class="comment">//窗口类结构</span></span><br><span class="line"></span><br><span class="line">	wndclass.style = CS_HREDRAW | CS_VREDRAW;                     <span class="comment">//窗口风格, CS_HERDRAW表示水平调整窗口会发生重绘</span></span><br><span class="line">	wndclass.lpfnWndProc = WndProc;                               <span class="comment">//窗口过程</span></span><br><span class="line">	wndclass.cbClsExtra = <span class="number">0</span>;                                      <span class="comment">//预留的额外空间</span></span><br><span class="line">	wndclass.cbWndExtra = <span class="number">0</span>;                                      <span class="comment">//</span></span><br><span class="line">	wndclass.hInstance = hInstance;                               <span class="comment">//实例句柄</span></span><br><span class="line">	wndclass.hIcon = LoadIcon(<span class="literal">NULL</span>, IDI_APPLICATION);             <span class="comment">//图标</span></span><br><span class="line">	wndclass.hCursor = LoadCursor(<span class="literal">NULL</span>, IDC_ARROW);               <span class="comment">//光标</span></span><br><span class="line">	wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE+<span class="number">1</span>); <span class="comment">//背景</span></span><br><span class="line">	wndclass.lpszClassName = szAppName;                           <span class="comment">//窗口类的名字</span></span><br><span class="line">	wndclass.lpszMenuName = <span class="literal">NULL</span>;                                 <span class="comment">//菜单名字</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//注册窗口，传入窗口类结构指针</span></span><br><span class="line">	<span class="keyword">if</span> (!RegisterClass(&amp;wndclass)) &#123;<span class="comment">//返回NULL</span></span><br><span class="line">		MessageBox(<span class="literal">NULL</span>, TEXT(<span class="string">"这个改程序需要在Windows NT才能执行"</span>), szAppName, MB_ICONERROR);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//创建窗口</span></span><br><span class="line">	hwnd = CreateWindow(szAppName,  <span class="comment">//窗口类名称</span></span><br><span class="line">		TEXT(<span class="string">"翻牌子"</span>),               <span class="comment">//窗口标题</span></span><br><span class="line">		WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU,        <span class="comment">//窗口风格</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y坐标</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始x方向尺寸</span></span><br><span class="line">		CW_USEDEFAULT,              <span class="comment">//初始y方向尺寸</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//父窗口句柄</span></span><br><span class="line">		<span class="literal">NULL</span>,                       <span class="comment">//窗口菜单句柄</span></span><br><span class="line">		hInstance,                  <span class="comment">//程序实例句柄</span></span><br><span class="line">		<span class="literal">NULL</span>);                      <span class="comment">//创建参数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">//显示窗口</span></span><br><span class="line">	ShowWindow(hwnd, iCmdShow);</span><br><span class="line">	<span class="comment">//更新窗口</span></span><br><span class="line">	UpdateWindow(hwnd);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//消息循环</span></span><br><span class="line">	<span class="keyword">while</span> (GetMessage(&amp;msg, <span class="literal">NULL</span>, <span class="number">0</span>, <span class="number">0</span>)) &#123;<span class="comment">//WM_QUIT消息，返回0</span></span><br><span class="line">		TranslateMessage(&amp;msg);</span><br><span class="line">		DispatchMessage(&amp;msg);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> msg.wParam;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function">LRESULT CALLBACK <span class="title">WndProc</span><span class="params">(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)</span> </span>&#123;</span><br><span class="line">	HDC hdc;</span><br><span class="line">	PAINTSTRUCT  ps;</span><br><span class="line">	RECT rect;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span>  cxChar, cyChar;</span><br><span class="line">	<span class="keyword">int</span> i, id;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> winner = <span class="number">-1</span>;</span><br><span class="line">	TCHAR * girls[NUM] = &#123; TEXT(<span class="string">"王昭君"</span>), TEXT(<span class="string">"杨玉环"</span>), TEXT(<span class="string">"西施"</span>), TEXT(<span class="string">"貂蝉"</span>), TEXT(<span class="string">"苍老师"</span>) &#125;;</span><br><span class="line">	<span class="keyword">static</span> HWND hwndButton[NUM], hwndRandomButton, hwndFuliButton, hwndGroupBox;</span><br><span class="line">	<span class="keyword">switch</span> (message) &#123;</span><br><span class="line">	<span class="keyword">case</span> WM_CREATE:</span><br><span class="line">		cxChar = LOWORD(GetDialogBaseUnits()); <span class="comment">//GetDialogBaseUnits()返回的值就是字体的大小，低16位为宽</span></span><br><span class="line">		cyChar = HIWORD(GetDialogBaseUnits()); <span class="comment">//高16为高</span></span><br><span class="line"></span><br><span class="line">		GetWindowRect(hwnd, &amp;rect);</span><br><span class="line">		MoveWindow(hwnd, rect.left, rect.top, <span class="number">27</span> * cxChar, <span class="number">13</span> * cyChar, TRUE);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; NUM<span class="number">-1</span>; ++i) &#123;</span><br><span class="line">			hwndButton[i] = CreateWindow(</span><br><span class="line">			TEXT(<span class="string">"button"</span>),</span><br><span class="line">				girls[i],</span><br><span class="line">				BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE,</span><br><span class="line">				<span class="number">2</span> * cxChar, cyChar * (<span class="number">2</span> + <span class="number">2</span>*i),</span><br><span class="line">				<span class="number">10</span> * cxChar, <span class="number">7</span> * cyChar / <span class="number">4</span>,</span><br><span class="line">				hwnd, </span><br><span class="line">				(HMENU)i,</span><br><span class="line">				((LPCREATESTRUCT)lParam)-&gt;hInstance, <span class="literal">NULL</span></span><br><span class="line">			);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		hwndButton[NUM - <span class="number">1</span>] = CreateWindow(</span><br><span class="line">			TEXT(<span class="string">"button"</span>),</span><br><span class="line">			girls[NUM - <span class="number">1</span>],</span><br><span class="line">			BS_AUTORADIOBUTTON | WS_CHILD,</span><br><span class="line">			<span class="number">2</span> * cxChar, cyChar * (<span class="number">2</span> * NUM ),</span><br><span class="line">			<span class="number">10</span> * cxChar, <span class="number">7</span> * cyChar / <span class="number">4</span>,</span><br><span class="line">			hwnd,</span><br><span class="line">			(HMENU)(NUM - <span class="number">1</span>),</span><br><span class="line">			((LPCREATESTRUCT)lParam)-&gt;hInstance, <span class="literal">NULL</span></span><br><span class="line">		);</span><br><span class="line"></span><br><span class="line">		hwndGroupBox = CreateWindow(</span><br><span class="line">			TEXT(<span class="string">"button"</span>),</span><br><span class="line">			TEXT(<span class="string">"妹子们"</span>),</span><br><span class="line">			BS_GROUPBOX | WS_CHILD | WS_VISIBLE,</span><br><span class="line">			cxChar, cyChar / <span class="number">2</span>,</span><br><span class="line">			<span class="number">12</span> * cxChar, <span class="number">10</span> * cyChar,</span><br><span class="line">			hwnd,</span><br><span class="line">			(HMENU)(NUM+<span class="number">1</span>),</span><br><span class="line">			((LPCREATESTRUCT)lParam)-&gt;hInstance, <span class="literal">NULL</span></span><br><span class="line">		);</span><br><span class="line"></span><br><span class="line">		CreateWindow(</span><br><span class="line">			TEXT(<span class="string">"button"</span>),</span><br><span class="line">			TEXT(<span class="string">"提交"</span>),</span><br><span class="line">			BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,</span><br><span class="line">			<span class="number">14</span> * cxChar, cyChar,</span><br><span class="line">			<span class="number">10</span> * cxChar, <span class="number">7</span> * cyChar / <span class="number">4</span>,</span><br><span class="line">			hwnd,</span><br><span class="line">			(HMENU)(NUM + <span class="number">2</span>),</span><br><span class="line">			((LPCREATESTRUCT)lParam)-&gt;hInstance, <span class="literal">NULL</span></span><br><span class="line">		);</span><br><span class="line"></span><br><span class="line">		hwndRandomButton= CreateWindow(</span><br><span class="line">			TEXT(<span class="string">"button"</span>),</span><br><span class="line">			TEXT(<span class="string">"帮我选择"</span>),</span><br><span class="line">			BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,</span><br><span class="line">			<span class="number">14</span> * cxChar, <span class="number">3</span> * cyChar,</span><br><span class="line">			<span class="number">10</span> * cxChar, <span class="number">7</span> * cyChar / <span class="number">4</span>,</span><br><span class="line">			hwnd,</span><br><span class="line">			(HMENU)(NUM + <span class="number">3</span>),</span><br><span class="line">			((LPCREATESTRUCT)lParam)-&gt;hInstance, <span class="literal">NULL</span></span><br><span class="line">		);</span><br><span class="line"></span><br><span class="line">		hwndFuliButton = CreateWindow(</span><br><span class="line">			TEXT(<span class="string">"button"</span>),</span><br><span class="line">			TEXT(<span class="string">"隐藏福利"</span>),</span><br><span class="line">			BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_DISABLED,</span><br><span class="line">			<span class="number">14</span> * cxChar, <span class="number">5</span> * cyChar,</span><br><span class="line">			<span class="number">10</span> * cxChar, <span class="number">7</span> * cyChar / <span class="number">4</span>,</span><br><span class="line">			hwnd,</span><br><span class="line">			(HMENU)(NUM + <span class="number">4</span>),</span><br><span class="line">			((LPCREATESTRUCT)lParam)-&gt;hInstance, <span class="literal">NULL</span></span><br><span class="line">		);</span><br><span class="line"></span><br><span class="line">		CreateWindow(</span><br><span class="line">			TEXT(<span class="string">"button"</span>),</span><br><span class="line">			TEXT(<span class="string">"隐藏按钮"</span>),</span><br><span class="line">			BS_OWNERDRAW | WS_CHILD | WS_VISIBLE,</span><br><span class="line">			<span class="number">14</span> * cxChar, <span class="number">7</span> * cyChar,</span><br><span class="line">			<span class="number">10</span> * cxChar, <span class="number">7</span> * cyChar / <span class="number">4</span>,</span><br><span class="line">			hwnd,</span><br><span class="line">			(HMENU)(NUM + <span class="number">5</span>),</span><br><span class="line">			((LPCREATESTRUCT)lParam)-&gt;hInstance, <span class="literal">NULL</span></span><br><span class="line">		);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_COMMAND:</span><br><span class="line">		id = LOWORD(wParam);</span><br><span class="line">		<span class="keyword">if</span> (id &lt; NUM) &#123;</span><br><span class="line">			winner = id;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">switch</span> (id) &#123; </span><br><span class="line">			<span class="comment">//提交按钮</span></span><br><span class="line">		<span class="keyword">case</span> NUM + <span class="number">2</span>:</span><br><span class="line">			<span class="keyword">if</span> (winner == <span class="number">-1</span>) &#123;</span><br><span class="line">				MessageBox(hwnd, TEXT(<span class="string">"启奏陛下，未有妃子被选中"</span>), TEXT(<span class="string">"启奏"</span>), MB_OK);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span> &#123;</span><br><span class="line">				MessageBox(hwnd, girls[winner], TEXT(<span class="string">"本次临幸的妃子"</span>), MB_OK);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">			<span class="comment">//帮我选择按钮</span></span><br><span class="line">		<span class="keyword">case</span> NUM+<span class="number">3</span>:</span><br><span class="line">			SendMessage(GetDlgItem(hwnd, winner), BM_SETCHECK, BST_UNCHECKED, <span class="number">0</span>); <span class="comment">//将之前选择的按钮去掉选中</span></span><br><span class="line">			<span class="comment">//获取随机 的id值</span></span><br><span class="line">			srand((<span class="keyword">unsigned</span> <span class="keyword">int</span>)time(<span class="literal">NULL</span>));</span><br><span class="line">			winner = rand() % (NUM - <span class="number">1</span>);			</span><br><span class="line">			SendMessage(GetDlgItem(hwnd, winner), BM_SETCHECK, BST_CHECKED, <span class="number">0</span>);<span class="comment">//设置随机选择的按钮选中</span></span><br><span class="line"></span><br><span class="line">			EnableWindow(hwndRandomButton, FALSE); <span class="comment">//禁止按钮</span></span><br><span class="line">			<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; NUM; ++i) &#123;</span><br><span class="line">				EnableWindow(GetDlgItem(hwnd, i), FALSE);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">			<span class="comment">//隐藏福利按钮</span></span><br><span class="line">		<span class="keyword">case</span> NUM+<span class="number">4</span>:</span><br><span class="line">			<span class="comment">//调整窗口尺寸</span></span><br><span class="line">			GetWindowRect(hwnd, &amp;rect);</span><br><span class="line">			MoveWindow(hwnd, rect.left, rect.top, <span class="number">27</span> * cxChar, <span class="number">15</span> * cyChar, TRUE);</span><br><span class="line"></span><br><span class="line">			<span class="comment">//调整GroupBox的尺寸</span></span><br><span class="line">			MoveWindow(hwndGroupBox, cxChar, cyChar / <span class="number">2</span>, <span class="number">12</span> * cxChar, <span class="number">12</span> * cyChar, TRUE);</span><br><span class="line">			<span class="comment">//将隐藏的选中按钮显示出来</span></span><br><span class="line">			ShowWindow(hwndButton[NUM - <span class="number">1</span>], SW_SHOWNORMAL);</span><br><span class="line"></span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">			<span class="comment">//隐藏按钮</span></span><br><span class="line">		<span class="keyword">case</span> NUM+<span class="number">5</span>:</span><br><span class="line">			<span class="comment">//将隐藏福利按钮设为可用</span></span><br><span class="line">			EnableWindow(hwndFuliButton, TRUE);</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">case</span> WM_DESTROY:</span><br><span class="line">		PostQuitMessage(<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> DefWindowProc(hwnd, message, wParam, lParam);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>运行结果：</p>
<p><img src="https://raw.githubusercontent.com/zxpgo/images/master/img/image-20200507154425806.png" alt="image-20200507154425806"></p>

      
    </div>
    
    
    

    
      <div>
        <div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center">
    <img id="wechat_subscriber_qcode" src="/images/wechat-qcode.jpg" alt="zxp wechat" style="width: 200px; max-width: 100%;"/>
    <div>欢迎关注微信公众号！</div>
</div>

      </div>
    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div></div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/WeChatpay.jpg" alt="zxp 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/Alipay.jpg" alt="zxp 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    zxp
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://zxpgo.github.io/2020/05/01/windows编程/" title="windows编程">https://zxpgo.github.io/2020/05/01/windows编程/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tag/windows-C/" rel="tag"># windows - C++</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/04/25/树各种算法的非递归实现/" rel="next" title="树的各种算法非递归实现">
                <i class="fa fa-chevron-left"></i> 树的各种算法非递归实现
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2021/08/13/VPN资源md/" rel="prev" title="梯子资源">
                梯子资源 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zODgxNC8xNTM0Mg=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.gif"
                alt="zxp" />
            
              <p class="site-author-name" itemprop="name">zxp</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">176</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">48</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/qq_25774883" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-globe"></i>CSDN</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/zxpgo/zxpgo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-globe"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.linkedin.com/feed/" target="_blank" title="LinkedIn">
                      
                        <i class="fa fa-fw fa-globe"></i>LinkedIn</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="1165772354@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://theme-next.iissnan.com" title="Next主题" target="_blank">Next主题</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://blog.rexking6.top" title="青爷博客" target="_blank">青爷博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://me.csdn.net/download/qq_25774883" title="CSDN下载" target="_blank">CSDN下载</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.livere.com/" title="来必力" target="_blank">来必力</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://tongji.baidu.com/web/welcome/login" title="百度统计" target="_blank">百度统计</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://leancloud.cn/" title="LeanCloud" target="_blank">LeanCloud</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://ibruce.info/2015/04/04/busuanzi/" title="不蒜子" target="_blank">不蒜子</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://leetcode-cn.com/" title="LeetCode" target="_blank">LeetCode</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Unicode解决方案"><span class="nav-number">1.</span> <span class="nav-text">Unicode解决方案</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#窗口"><span class="nav-number">2.</span> <span class="nav-text">窗口</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#文本输出"><span class="nav-number">3.</span> <span class="nav-text">文本输出</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#字符串处理"><span class="nav-number">4.</span> <span class="nav-text">字符串处理</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#获取窗口的各种尺寸"><span class="nav-number">5.</span> <span class="nav-text">获取窗口的各种尺寸</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#滚动条"><span class="nav-number">6.</span> <span class="nav-text">滚动条</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#获取客户区的尺寸"><span class="nav-number">6.1.</span> <span class="nav-text">获取客户区的尺寸</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#添加滚动条"><span class="nav-number">6.2.</span> <span class="nav-text">添加滚动条</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#窗口绘画"><span class="nav-number">7.</span> <span class="nav-text">窗口绘画</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#画直线"><span class="nav-number">7.1.</span> <span class="nav-text">画直线</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#折线"><span class="nav-number">7.2.</span> <span class="nav-text">折线</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#画笔"><span class="nav-number">8.</span> <span class="nav-text">画笔</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#画刷"><span class="nav-number">9.</span> <span class="nav-text">画刷</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#多边形"><span class="nav-number">10.</span> <span class="nav-text">多边形</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#映射模式"><span class="nav-number">11.</span> <span class="nav-text">映射模式</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#视口与窗口"><span class="nav-number">12.</span> <span class="nav-text">视口与窗口</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#处理矩形"><span class="nav-number">13.</span> <span class="nav-text">处理矩形</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#随机矩形"><span class="nav-number">14.</span> <span class="nav-text">随机矩形</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#区域和裁剪"><span class="nav-number">15.</span> <span class="nav-text">区域和裁剪</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#创建区域："><span class="nav-number">15.1.</span> <span class="nav-text">创建区域：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#键盘"><span class="nav-number">16.</span> <span class="nav-text">键盘</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#插入符号"><span class="nav-number">17.</span> <span class="nav-text">插入符号</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#客户区鼠标消息"><span class="nav-number">18.</span> <span class="nav-text">客户区鼠标消息</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#非客户区鼠标消息"><span class="nav-number">19.</span> <span class="nav-text">非客户区鼠标消息</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#键盘模拟鼠标操作"><span class="nav-number">20.</span> <span class="nav-text">键盘模拟鼠标操作</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#子窗口"><span class="nav-number">21.</span> <span class="nav-text">子窗口</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#捕获鼠标"><span class="nav-number">22.</span> <span class="nav-text">捕获鼠标</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#鼠标滚轮"><span class="nav-number">23.</span> <span class="nav-text">鼠标滚轮</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#计时器"><span class="nav-number">24.</span> <span class="nav-text">计时器</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#各种按钮"><span class="nav-number">25.</span> <span class="nav-text">各种按钮</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div>
<script async src="https//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_pv" style='display:none'>
    本站总访问量 <span id="busuanzi_value_site_pv"></span> 
    <span class="post-meta-divider">|</span>
</span>
<span id="busuanzi_container_site_uv" style='display:none'>
    访问人数 <span id="busuanzi_value_site_uv"></span>
</span>
</div>


<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; 2018-8 &mdash; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-"></i> Power by 
  </span>
  <span class="author" itemprop="copyrightHolder">zxp</span>
  
  
</div>









        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
      <div id="needsharebutton-float">
        <span class="btn">
          <i class="fa fa-share-alt" aria-hidden="true"></i>
        </span>
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  




  
  <div id="lv-container" data-uid="MTAyMC8zODgxNC8xNTM0Mg==">
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
	</div>
  











  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("2AyV3DKioBSdoryrFLRohzjB-gzGzoHsz", "XynedcHyJCVCrTfbD4yYnodo");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "default";
      
          pbOptions.boxForm = "vertical";
      
          pbOptions.position = "top";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
      flOptions = {};
      
          flOptions.iconStyle = "box";
      
          flOptions.boxForm = "horizontal";
      
          flOptions.position = "middleRight";
      
          flOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-float', flOptions);
    
  </script>

  

  
  


  
  <script type="text/javascript" src="/js/src/js.cookie.js?v=5.1.4"></script>
  <script type="text/javascript" src="/js/src/scroll-cookie.js?v=5.1.4"></script>


  
  <script type="text/javascript" src="/js/src/exturl.js?v=5.1.4"></script>


  
  
  	 <!-- custom analytics part create by xiamo -->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("2AyV3DKioBSdoryrFLRohzjB-gzGzoHsz", "XynedcHyJCVCrTfbD4yYnodo");</script>
<script>
function showTime(Counter) {
	var query = new AV.Query(Counter);
	$(".leancloud_visitors").each(function() {
		var url = $(this).attr("id").trim();
		query.equalTo("url", url);
		query.find({
			success: function(results) {
				if (results.length == 0) {
					var content = $(document.getElementById(url)).text() + ' 0';
					$(document.getElementById(url)).text(content);
					return;
				}
				for (var i = 0; i < results.length; i++) {
					var object = results[i];
					var content = $(document.getElementById(url)).text() + ' ' + object.get('time');
					$(document.getElementById(url)).text(content);
				}
			}
		});

	});
}

</script>
  
</body>
</html>
